Java 开源常用工具类

开源工具类:


一、字符串操作类

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.10</version>
</dependency>

a.判断空值

if (StringUtils.isEmpty(str)) {

}

StringUtils 内部还有一个方法 isBlank,也是用来判断字符串是否为空,两个方法比较相近,区别如下:
//字符串都是空格
StringUtils.isBlank(" ")       = true;
StringUtils.isEmpty(" ")       = false;   

b.字符串关键字替换

// 默认替换所有关键字 zbz
System.out.println(StringUtils.replace("aba", "a", "z"));
// 替换关键字,仅替换一次 zba
System.out.println(StringUtils.replaceOnce("aba", "a", "z"));
// 使用正则表达式替换 ABC123
System.out.println(StringUtils.replacePattern("ABCabc123", "[^A-Z0-9]+", ""));

c.日期格式化

// Date 转化为字符串
String sd = DateFormatUtils.format(new Date(),"yyyy-MM-dd HH:mm:ss");
System.out.println(sd);
// 字符串 转 Date
try {
    Date aDate = DateUtils.parseDate("2020-05-07 22:00:00","yyyy-MM-dd HH:mm:ss");
    System.out.println(aDate);
} catch (ParseException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

d.数组操作类

int[] array = {};
// 数组判空
if (ArrayUtils.isEmpty(array)) {
    System.out.println("空");
}

二、集合、列表操作类

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.4</version>
</dependency>

List<String> list = new ArrayList<String>();
// List/Set 集合判空
if(CollectionUtils.isEmpty(list)){
    System.out.println("空");
}
Map<String, String> map = new HashedMap<String, String>();
// Map 等集合进行判空
if (MapUtils.isEmpty(map)) {
    System.out.println("空");
}

三、IO操作相关
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

try {
    //a.拷贝文件
    File fileA = new File("D:\\apache-maven-3.5.4.zip");
    File fileB = new File("D:\\test\\test.zip");
    FileUtils.copyFile(fileA,fileB);

    //b.使用 FileUtils.readLines 读取该文件所有行。
    File fileC = new File("D:\\test\\cc.txt");
    //读取指定文件所有行 不需要使用 while 循环读取流了
    List<String> lines = FileUtils.readLines(fileC, "utf-8");
    for (String string : lines) {
        System.out.println(string);
    }
    
    //c.使用 FileUtils.writeLines,直接将集合中数据,一行行写入文本。
    FileUtils.writeLines(new File("D:\\test\\bb.txt"), lines);
    
    //d.将输入流信息全部输出到字节数组中
    byte[] b = IOUtils.toByteArray(request.getInputStream());
    // 将输入流信息转化为字符串
    String resMsg = IOUtils.toString(request.getInputStream());
    
} catch (IOException e1) {
    e1.printStackTrace();
}


四、计时
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>29.0-jre</version>
</dependency>

// 创建之后立刻计时
Stopwatch stopwatch = Stopwatch.createStarted();
// 创建计时器,需要主动调用 start 方法开始计时
//Stopwatch stopwatch = Stopwatch.createUnstarted();
//stopwatch.start();
// 模拟代码耗时
TimeUnit.SECONDS.sleep(5l);
// 停止计时 未开始的计时器调用 stop 将会抛错 IllegalStateException
stopwatch.stop();
// 再次统计总耗时
System.out.println(stopwatch.elapsed(TimeUnit.SECONDS));;
// 重新开始,将会在原来时间基础计算,若想重新从 0开始计算,需要调用 stopwatch.reset()
stopwatch.reset();
stopwatch.start();
TimeUnit.SECONDS.sleep(2l);
System.out.println(stopwatch.elapsed(TimeUnit.SECONDS));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值