博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Apache Commons 常用工具类整理
阅读量:6981 次
发布时间:2019-06-27

本文共 5648 字,大约阅读时间需要 18 分钟。

其实一直都在使用常用工具类,只是从没去整理过,今天空了把一些常用的整理一下吧

怎么使用的一看就明白,另外还有注释,最后的使用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
om = new LinkedMap
(); 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
bag = new HashBag(); bag.add("abc"); bag.add("def", 3); bag.add("ghi", 5); System.out.println(bag.size()); // 过滤重复元素 Set onlyU = bag.uniqueSet(); Iterator 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("")); 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)); } }

 

commons-codec
commons-codec
1.10
org.apache.commons
commons-collections4
4.1
commons-configuration
commons-configuration
1.10

 

附上地址:

 

转载于:https://www.cnblogs.com/leechenxiang/p/5288177.html

你可能感兴趣的文章
mongoDB Indexing Advice
查看>>
跟我学Kafka之NIO通信机制
查看>>
关于版本管理
查看>>
1 Java NIO概述-翻译
查看>>
图像延迟加载 && 列表图顺序加载
查看>>
MySQL 基础架构你不知道的那些事!
查看>>
Vue学习笔记(1)——在页面右上角实现可悬浮/隐藏的系统菜单
查看>>
UITableView占位图的低耦合性设计
查看>>
一个女装小程序的瀑布流实现
查看>>
Docker实现Mariadb分库分表、读写分离
查看>>
Node.js核心内容
查看>>
github克隆本地项目
查看>>
j抽奖
查看>>
GMQ力争为全球区块链数字资产技术应用贡献一份力量
查看>>
VUE+Vant 实现图片上传
查看>>
ajax实现点击加载更多
查看>>
为什么JavaScript没有类而使用原型?——JavaScript语言特性来历
查看>>
TarsGo新版本发布,支持protobuf,zipkin和自定义插件
查看>>
Flutter 如何创建并发布 Plugin (VS Code + GitHub 发布)
查看>>
TableStore实战:GEO索引打造亿量级店铺搜索系统
查看>>