设为首页 收藏本站
查看: 922|回复: 0

[经验分享] Apache Commons 常用工具类整理

[复制链接]
发表于 2017-12-24 13:08:33 | 显示全部楼层 |阅读模式
public>
/**  * 从一个entity中把属性复制进另外一个entity中
  *
  *
@throws Exception*/  @Test
public void testCopyNewBean() throws Exception {  StuForm form
= new StuForm("lee", 18, 1, new Date(), true);  Stu stu
= new Stu();  BeanUtils.copyProperties(form, stu);
  System.out.println(stu.toString());
  }
/**  * base64 加密解密
  *
  *
@throws Exception*/  @Test
public void testBase64Code() throws Exception {  String name1
= "hello, my name is lee~";  System.out.println(
"Before: " + name1);  String name2
= Base64.encodeBase64String(name1.getBytes());  System.out.println(
"After encode: " + name2);  String name3
= new String(Base64.decodeBase64(name2));  System.out.println(
"After decode: " + name3);  String url1
= "www.lee.com.cn";  System.out.println(
"URL Before: " + url1);  String url2
= Base64.encodeBase64URLSafeString(url1.getBytes());  System.out.println(
"URL After decode: " + url2);  String url3
= new String(Base64.decodeBase64(url2));  System.out.println(
"URL After decode: " + url3);  }
/**  * commons 下 collection 工具包
  *
  *
@throws Exception*/  @Test
public void testCollection() throws Exception {  OrderedMap
<String, Object> om = new LinkedMap<String, Object>();  om.put(
"one", 1);  om.put(
"two", "2");  om.put(
"three", "three");  om.put(
"fore", 4);  om.put(
"five", "5");  System.out.println(om.firstKey());
  System.out.println(om.nextKey(
"fore"));  System.out.println(om.previousKey(
"five"));  System.out.println(
"==============================");  BidiMap bm
= new TreeBidiMap();  bm.put(
"three", "3");  bm.put(
"five", "isfive");  System.out.println(bm.getKey(
"isfive").toString());  System.out.println(bm.get(
"three"));// 交换key和value  BidiMap newMap = bm.inverseBidiMap();
  System.out.println(newMap.size());
  System.out.println("==============================");
  Bag<Object> bag = new HashBag<Object>();
  bag.add("abc");
  bag.add("def", 3);
  bag.add("ghi", 5);
  System.out.println(bag.size());
  // 过滤重复元素
  Set<Object> onlyU = bag.uniqueSet();
  Iterator<Object> i = onlyU.iterator();
  while(i.hasNext()){
  Object o = i.next();
  System.out.println(o.toString());
  }
  }
  /**
  * Apache Commons Configuration
  *
  * @throws Exception
  */
  @Test
  public void testConfig() throws Exception {
  PropertiesConfiguration p = new PropertiesConfiguration("test.properties");
  System.out.println(p.getString("boy.name"));
  System.out.println(p.getInt("boy.age"));
  System.out.println(p.getString("boy.birth"));
  p.setHeader("##this is a new string##");
  p.setProperty("new.string", "newString");
  // 保存在编译后的目录中
  
        p.save();
  p.save("newP");
  }
  /**
  * Apache Commons Lang
  *
  * @throws Exception
  */
  @Test
  public void testLang() throws Exception {
  String a1[] = {"1", "2", "3"};
  String a2[] = {"a", "b", "c"};
  // 合并数组
  String a3[] = (String[])ArrayUtils.addAll(a1, a2);
  for (String s : a3) {
  System.out.println(s);
  }
  System.out.println("==============================");
  String str = "hello, my name is hanmeimei! what's your name? name";
  // 出现第一个和第二个name之间的string
  String s1 = StringUtils.substringBetween(str, "name");
  System.out.println("s1: " + s1);
  // 截取第一次出现的字符串之间的string
  String s2 = StringUtils.substringBetween(str, "name", "your");
  System.out.println("s2: " + s2);
  
//        StringUtils.substringAfter(str, separator)
  
//        StringUtils.substringBefore(str, separator)
  
        
  System.out.println("==============================");
  // 判断该字符串是不是为数字(0~9)组成,如果是,返回true 但该方法不识别有小数点
  System.out.println(StringUtils.isNumeric("454534"));
  System.out.println("==============================");
  System.out.println(ClassUtils.getShortClassName(Test.class));
  System.out.println(ClassUtils.getPackageName(Test.class));
  System.out.println("==============================");
  // 判断该字符串是不是为数字(0~9)组成,如果是,返回true 可以识别有小数点
  System.out.println(NumberUtils.isNumber("12334.11"));
  // 不建议使用,可以使用 Integer.valueOf("[number]")
  System.out.println(NumberUtils.stringToInt("33"));
  System.out.println(Integer.valueOf("33"));
  // 五位的随机字母和数字
  System.out.println(RandomStringUtils.randomAlphanumeric(5));
  System.out.println(StringEscapeUtils.escapeHtml("<html>"));
  System.out.println(StringEscapeUtils.escapeJava("String"));
  // StringUtils,判断是否是空格字符
  System.out.println(StringUtils.isBlank("   "));
  
//        StringUtils.isEmpty("");
  // 将数组中的内容以,分隔
  System.out.println(StringUtils.join(a3, ","));
  // 在右边加下字符,使之总长度为6
  System.out.println(StringUtils.rightPad("abc", 6, 'T'));
  // 首字母大写
  System.out.println(StringUtils.capitalize("abc"));
  // Deletes all whitespaces from a String 删除所有空格
  System.out.println(StringUtils.deleteWhitespace("   ab  c  "));
  // 判断是否包含这个字符
  System.out.println(StringUtils.contains("abc", "ba"));
  // 表示左边两个字符
  System.out.println(StringUtils.left("abc", 2));
  }
  
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-427510-1-1.html 上篇帖子: Apache 2.4.28的安装 下篇帖子: Apache Nginx URL 地址 重写
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表