其实一直都在使用常用工具类,只是从没去整理过,今天空了把一些常用的整理一下吧
怎么使用的一看就明白,另外还有注释,最后的使用pom引入的jar包

public class ApacheCommonsTest {

    /**
* 从一个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));
} }
<!-- apache commons -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.1</version>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.10</version>
</dependency>

附上地址:https://github.com/leechenxiang/maven-apache-commons

Apache Commons 常用工具类整理的更多相关文章

  1. Maven基础&&Spring框架阶段常用工具类整理

    常用工具类 1.密码加密工具类: package com.itheima.utils; import java.security.MessageDigest; import sun.misc.BASE ...

  2. javascript常用工具类整理(copy)

    JavaScript常用工具类 类型 日期 数组 字符串 数字 网络请求 节点 存储 其他 1.类型 isString (o) { //是否字符串 return Object.prototype.to ...

  3. Maven 常用工具类整理

    目录 1.Apache Commons 1.1.字符串处理 1.2.集合操作 1.3.IO操作 1.4.编解码操作 2.Google Guava 2.1.多场景使用 2.2.guava-retryin ...

  4. Java常用工具类整理

    字符数组转String package com.sunsheen.hcc.fabric.utils; /** * 字符数组工具 * @author WangSong * */ public class ...

  5. org.apache.commons.httpclient工具类

    import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler; import org.apache.commons.httpcl ...

  6. org.apache.commons.httpclient工具类(封装的HttpUtil)

    import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java ...

  7. IOS开发--常用工具类收集整理(Objective-C)(持续更新)

    前言:整理和收集了IOS项目开发常用的工具类,最后也给出了源码下载链接. 这些可复用的工具,一定会给你实际项目开发工作锦上添花,会给你带来大大的工作效率. 重复造轮子的事情,除却自我多练习编码之外,就 ...

  8. commons-lang3-3.2.jar中的常用工具类的使用

    这个包中的很多工具类可以简化我们的操作,在这里简单的研究其中的几个工具类的使用. 1.StringUtils工具类 可以判断是否是空串,是否为null,默认值设置等操作: /** * StringUt ...

  9. commons-lang常用工具类StringEscapeUtils使用--转

    https://my.oschina.net/ydsakyclguozi/blog/341496 在apache commons-lang(2.3以上版本)中为我们提供了一个方便做转义的工具类,主要是 ...

随机推荐

  1. ahjesus解决win下U盘无法写入的问题

    可能是由于不同品牌的U盘出厂时磁盘分区和格式化方式不同而引起的兼容性问题.解决方案如下 启动cmd.输入diskpart,启动DISKPART工具 在DISKPART窗口中输入以下命令: >li ...

  2. mysql bin-log和innodb_log的关系

    首先,二进制日志会记录所有与MySQL数据库有关的日志记录,包括InnoDB.MyISAM.Heap(memory除外)等其他存储引擎的日志.而InnoDB存储引擎的重做日志记录有关该引擎本身的事务日 ...

  3. jQuery Mobile笔记

    1.获取jQuery mobile 文件,访问jQuerymobile网站下载 (貌似使用jquery mobile后,jquery会自动在网页中添加一些class类,第一次知道的我是被吓呆的!!) ...

  4. idoc 和 bapi 和 rfc 之间的区别

        se37 写出来的叫function,其中可以远程调用的叫rfc,remote-enabled function,abap语法和输入输出参数就会有一些限制.bapi是sap做好的实现特定业务操 ...

  5. Sharepoint学习笔记—习题系列--70-573习题解析 -(Q54-Q56)

    Question 54You create custom code to import content to SharePoint sites.You create a custom site def ...

  6. GitHub 基本常用知识解答2

    1.如何拥有一个Git仓库的两种途径 (1)在已有的目录中,初始化一个新的. (2) 比如一个新的项目,或者一个已存在的项目,但该项目尚未有版本控制.如果你想要复制一份别人的项目, 或者与别人合作某个 ...

  7. 一个基于Myeclipse开发的Java打地鼠小游戏(Appletcation)

    package javaes.zixue.wangshang.daima; 2 3 import java.awt.Cursor; import java.awt.Image; import java ...

  8. Eclipse环境下配置spket中ExtJS5.0提示

    使用eclipse编写extjs时,一定会用到spket这个插件,spket可以单独当作ide使用,也可以当作eclipse插件使用,我这里是当作eclipse的插件使用的,下面来一步步图解说明如何配 ...

  9. CSS标签选择器(二)

    一.CSS选择器概述 1.1.CSS功能 CSS语言具有两个基本功能:匹配和渲染 当浏览器在解析CSS样式时,首先应该确定哪些元素需要渲染,即匹配哪些HTML元素,这个操作由CSS样式中的选择器负责标 ...

  10. Silverlight项目笔记5:Oracle归档模式引起的异常&&表格控件绑定按钮

    1.Oracle归档模式产生日志文件引起数据库异常 连接数据库失败,提示监听错误,各种检查监听配置文件,删除再添加监听,无果. sqlplus下重启数据库数据库依然无果,期间碰到多个错误提示: ORA ...