Java 常用工具类大全:高频工具类及代码示例(后续继续补充)

ops/2025/3/18 23:16:56/

在 Java 开发中,工具类是提高开发效率的利器。本文将总结全网搜索频率最高的 20 个 Java 工具类,并提供详细的代码示例和说明,涵盖字符串处理、集合操作、日期时间、文件操作、数学计算等多个方面。


1. ​**StringUtils(Apache Commons Lang)​**

用于字符串处理的工具类。

 

java

import org.apache.commons.lang3.StringUtils;public class StringUtilsExample {public static void main(String[] args) {// 判断字符串是否为空System.out.println(StringUtils.isEmpty("")); // true// 判断字符串是否为空白System.out.println(StringUtils.isBlank("  ")); // true// 反转字符串System.out.println(StringUtils.reverse("hello")); // olleh}
}

2. ​**CollectionUtils(Apache Commons Collections)​**

用于集合操作的工具类。

 

java

import org.apache.commons.collections4.CollectionUtils;import java.util.Arrays;
import java.util.List;public class CollectionUtilsExample {public static void main(String[] args) {List<String> list1 = Arrays.asList("a", "b", "c");List<String> list2 = Arrays.asList("b", "c", "d");// 判断集合是否为空System.out.println(CollectionUtils.isEmpty(list1)); // false// 求交集System.out.println(CollectionUtils.intersection(list1, list2)); // [b, c]}
}

3. ​**DateUtils(Apache Commons Lang)​**

用于日期时间处理的工具类。

 

java

import org.apache.commons.lang3.time.DateUtils;import java.util.Date;public class DateUtilsExample {public static void main(String[] args) {Date now = new Date();// 添加天数Date future = DateUtils.addDays(now, 7);System.out.println(future);// 判断是否为同一天System.out.println(DateUtils.isSameDay(now, future)); // false}
}

4. ​**FileUtils(Apache Commons IO)​**

用于文件操作的工具类。

 

java

import org.apache.commons.io.FileUtils;import java.io.File;
import java.io.IOException;public class FileUtilsExample {public static void main(String[] args) throws IOException {// 复制文件FileUtils.copyFile(new File("source.txt"), new File("target.txt"));// 读取文件内容String content = FileUtils.readFileToString(new File("source.txt"), "UTF-8");System.out.println(content);}
}

5. ​**MathUtils(自定义)​**

用于数学计算的工具类。

 

java

public class MathUtils {// 计算两个数的最大公约数public static int gcd(int a, int b) {return b == 0 ? a : gcd(b, a % b);}// 计算两个数的最小公倍数public static int lcm(int a, int b) {return a * b / gcd(a, b);}public static void main(String[] args) {System.out.println(gcd(12, 18)); // 6System.out.println(lcm(12, 18)); // 36}
}

6. ​**IOUtils(Apache Commons IO)​**

用于 I/O 操作的工具类。

 

java

import org.apache.commons.io.IOUtils;import java.io.FileInputStream;
import java.io.IOException;public class IOUtilsExample {public static void main(String[] args) throws IOException {// 读取文件内容String content = IOUtils.toString(new FileInputStream("source.txt"), "UTF-8");System.out.println(content);}
}

7. ​**JsonUtils(Jackson)​**

用于 JSON 处理的工具类。

 

java

import com.fasterxml.jackson.databind.ObjectMapper;import java.io.IOException;public class JsonUtils {private static final ObjectMapper mapper = new ObjectMapper();// 将对象转换为 JSON 字符串public static String toJson(Object obj) throws IOException {return mapper.writeValueAsString(obj);}// 将 JSON 字符串转换为对象public static <T> T fromJson(String json, Class<T> clazz) throws IOException {return mapper.readValue(json, clazz);}public static void main(String[] args) throws IOException {String json = "{\"name\":\"John\",\"age\":30}";Person person = fromJson(json, Person.class);System.out.println(person.getName()); // John}
}class Person {private String name;private int age;// getters and setters
}

8. ​**ReflectionUtils(Spring Framework)​**

用于反射操作的工具类。

 

java

import org.springframework.util.ReflectionUtils;import java.lang.reflect.Method;public class ReflectionUtilsExample {public static void main(String[] args) {Method method = ReflectionUtils.findMethod(String.class, "length");System.out.println(method.getName()); // length}
}

9. ​**RandomUtils(Apache Commons Lang)​**

用于生成随机数的工具类。

 

java

import org.apache.commons.lang3.RandomUtils;public class RandomUtilsExample {public static void main(String[] args) {// 生成随机整数int randomInt = RandomUtils.nextInt(1, 100);System.out.println(randomInt);}
}

10. ​**StopWatch(Spring Framework)​**

用于计时操作的工具类。

 

java

import org.springframework.util.StopWatch;public class StopWatchExample {public static void main(String[] args) throws InterruptedException {StopWatch watch = new StopWatch();watch.start();Thread.sleep(1000);watch.stop();System.out.println(watch.getTotalTimeMillis()); // 1000}
}

11. ​**StringJoiner(Java 8+)​**

用于拼接字符串的工具类。

 

java

import java.util.StringJoiner;public class StringJoinerExample {public static void main(String[] args) {StringJoiner joiner = new StringJoiner(", ", "[", "]");joiner.add("a").add("b").add("c");System.out.println(joiner.toString()); // [a, b, c]}
}

12. ​**Optional(Java 8+)​**

用于处理空值的工具类。

 

java

import java.util.Optional;public class OptionalExample {public static void main(String[] args) {Optional<String> optional = Optional.ofNullable(null);System.out.println(optional.orElse("default")); // default}
}

13. ​**Objects(Java 7+)​**

用于对象操作的工具类。

 

java

import java.util.Objects;public class ObjectsExample {public static void main(String[] args) {String str = null;System.out.println(Objects.isNull(str)); // true}
}

14. ​**Arrays(Java 标准库)​**

用于数组操作的工具类。

 

java

import java.util.Arrays;public class ArraysExample {public static void main(String[] args) {int[] arr = {3, 1, 2};Arrays.sort(arr);System.out.println(Arrays.toString(arr)); // [1, 2, 3]}
}

15. ​**Collections(Java 标准库)​**

用于集合操作的工具类。

 

java

import java.util.Collections;
import java.util.List;public class CollectionsExample {public static void main(String[] args) {List<String> list = Arrays.asList("a", "b", "c");Collections.reverse(list);System.out.println(list); // [c, b, a]}
}

16. ​**Files(Java 7+)​**

用于文件操作的工具类。

 

java

import java.nio.file.Files;
import java.nio.file.Paths;
import java.io.IOException;public class FilesExample {public static void main(String[] args) throws IOException {// 读取文件内容String content = new String(Files.readAllBytes(Paths.get("source.txt")));System.out.println(content);}
}

17. ​**Path(Java 7+)​**

用于路径操作的工具类。

 

java

import java.nio.file.Path;
import java.nio.file.Paths;public class PathExample {public static void main(String[] args) {Path path = Paths.get("source.txt");System.out.println(path.getFileName()); // source.txt}
}

18. ​**Stream(Java 8+)​**

用于流式操作的工具类。

 

java

import java.util.stream.Stream;public class StreamExample {public static void main(String[] args) {Stream.of("a", "b", "c").forEach(System.out::println); // a b c}
}

19. ​**DateTimeFormatter(Java 8+)​**

用于日期时间格式化的工具类。

 

java

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;public class DateTimeFormatterExample {public static void main(String[] args) {LocalDateTime now = LocalDateTime.now();DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");System.out.println(now.format(formatter)); // 2023-10-01 12:34:56}
}

20. ​**Properties(Java 标准库)​**

用于读取配置文件的工具类。

 

java

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;public class PropertiesExample {public static void main(String[] args) throws IOException {Properties props = new Properties();props.load(new FileInputStream("config.properties"));System.out.println(props.getProperty("key")); // value}
}

http://www.ppmy.cn/ops/166893.html

相关文章

STM32配套程序接线图

1 工程模板 2 LED闪烁 3LED流水灯 4蜂鸣器 5按键控制LED 6光敏传感器控制蜂鸣器 7OLED显示屏 8对射式红外传感器计次 9旋转编码器计次 10 定时器定时中断 11定时器外部时钟 12PWM驱动LED呼吸灯 13 PWM驱动舵机 14 PWM驱动直流电机 15输入捕获模式测频率 16PWMI模式测频率占空…

jenkins 配置邮件问题整理

版本&#xff1a;Jenkins 2.492.1 插件&#xff1a; A.jenkins自带的&#xff0c; B.安装功能强大的插件 配置流程&#xff1a; 1. jenkins->系统配置->Jenkins Location 此处的”系统管理员邮件地址“&#xff0c;是配置之后发件人的email。 2.配置系统自带的邮件A…

SQL语言的编译原理

SQL语言的编译原理 引言 SQL&#xff08;Structured Query Language&#xff0c;结构化查询语言&#xff09;是用于管理和操作关系数据库的一种标准语言。作为一种高级语言&#xff0c;SQL不仅易于使用&#xff0c;而且功能强大。然而&#xff0c;SQL语言本身并不能直接被计算…

LeetCode2593 标记所有元素后数组的分数

贪心算法实战&#xff1a;数组标记与分数计算&#xff08;LeetCode 同类题解析&#xff09; 一、问题描述 给定一个正整数数组 nums&#xff0c;按以下规则计算最终分数&#xff1a; 初始分数 score 0每次选择最小且未被标记的元素&#xff08;值相同选下标最小&#xff09…

【最新版】智慧小区物业管理小程序源码+uniapp全开源

一.系统介绍 智慧小区物业管理小程序,包含小区物业缴费、房产管理、在线报修、业主活动报名、在线商城等功能。为物业量身打造的智慧小区运营管理系统,贴合物业工作场景,轻松提高物业费用收缴率,更有功能模块个性化组合,助力物业节约成本高效运营。 二.搭建环境 系统环…

Android第三次面试总结(activity和线程池)

1. Activity 的生命周期方法有哪些&#xff1f;调用顺序是什么&#xff1f; 回答思路&#xff1a;列举 7 个核心方法并说明其触发场景。回答示例&#xff1a; 完整生命周期&#xff1a;onCreate() → onStart() → onResume() → onPause() → onStop() → onDestroy()。可见但…

SpringMVC——REST简介及入门案例

REST简介 REST&#xff08;Representational State Transfer&#xff09;即表现层状态转移&#xff0c;是一种基于HTTP协议的网络应用程序的架构风格。它强调客户端和服务器之间的交互操作&#xff0c;通过对资源的表现形式进行操作来实现对资源的管理。REST风格的API设计具有简…

数学建模:常用模型

数学建模四大模型总结 数学建模常见模型整理&#xff08;简单介绍&#xff09; 建模流程&#xff1a; 首先&#xff0c;对题目分析&#xff0c;判断题目是属于哪一类建模问题。 再从对应分类的建模方法里面进行选择。&#xff08;查找文献&#xff0c;随机应变&#xff09;…