Apache Commons 常用工具类整理

简介: 其实一直都在使用常用工具类,只是从没去整理过,今天空了把一些常用的整理一下吧 怎么使用的一看就明白,另外还有注释,最后的使用pom引入的jar包   public class ApacheCommonsTest { /** * 从一个entity中把属性复制进...

其实一直都在使用常用工具类,只是从没去整理过,今天空了把一些常用的整理一下吧
怎么使用的一看就明白,另外还有注释,最后的使用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

 

相关文章
|
5月前
|
SQL 流计算 OceanBase
这个错误提示表明在运行时找不到`org.apache.flink.table.api.ValidationException`类
这个错误提示表明在运行时找不到`org.apache.flink.table.api.ValidationException`类
276 4
|
2月前
|
Shell
Flume【问题记录 01】【at org.apache.flume.node.Application.main(Application.java:xxx) 类问题整理+其他类型问题总结】【避坑指南】
【2月更文挑战第17天】Flume【问题记录 01】【at org.apache.flume.node.Application.main(Application.java:xxx) 类问题整理+其他类型问题总结】【避坑指南】
64 2
|
6月前
|
Apache
java.lang.NoClassDefFoundError: org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream
java.lang.NoClassDefFoundError: org/apache/commons/io/output/UnsynchronizedByteArrayOutputStream
168 0
|
4月前
|
分布式计算 Java 大数据
IO流【Java对象的序列化和反序列化、File类在IO中的作用、装饰器模式构建IO流体系、Apache commons-io工具包的使用】(四)-全面详解(学习总结---从入门到深化)
IO流【Java对象的序列化和反序列化、File类在IO中的作用、装饰器模式构建IO流体系、Apache commons-io工具包的使用】(四)-全面详解(学习总结---从入门到深化)
54 0
|
2月前
|
存储 测试技术 API
Apache Hudi 负载类Payload使用案例剖析
Apache Hudi 负载类Payload使用案例剖析
44 4
|
3月前
|
算法 Java Apache
Apache Commons
Apache Commons是一个开源项目,提供了一系列的工具和库,用于简化Java开发中的常见任务。
26 1
|
9月前
|
Java 数据库连接 Apache
Java 中Apache-DBUtils类的使用详解
Java 中Apache-DBUtils类的使用详解
60 0
|
9月前
|
SpringCloudAlibaba Java Nacos
空的springboot项目导入nacos 配置中心和注册中心依赖后报错,提示 org.apache.http.impl.client.HttpClientBuilder 这个类找不到 问题的解决
空的springboot项目导入nacos 配置中心和注册中心依赖后报错,提示 org.apache.http.impl.client.HttpClientBuilder 这个类找不到 问题的解决
205 0
|
12月前
|
移动开发 前端开发 Java
Spring MVC-09循序渐进之文件上传(基于Apache Commons FileUpload)
Spring MVC-09循序渐进之文件上传(基于Apache Commons FileUpload)
76 0
apache.commons.lang3常用工具类
apache.commons.lang3常用工具类
327 0

推荐镜像

更多