Java开发

日常开发中常用的 Java 工具类和库

2025-01-07  本文已影响0人  _浅墨_

日常开发中常用的 Java 工具类和库,按功能分类推荐,能大大提高开发效率:

1. 通用工具类

1.  Apache Commons Lang
•   提供丰富的工具类(如 StringUtils, NumberUtils, DateUtils, 等)。
•   例子:
String result = StringUtils.capitalize("hello"); // 输出: "Hello"
boolean isNumeric = StringUtils.isNumeric("12345"); // 输出: true
•   Maven 依赖:
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.12.0</version>
</dependency>

2. Guava

•   Google 出品的工具库,提供集合处理、缓存、字符串操作、并发工具等。
•   例子:
List<String> list = Lists.newArrayList("a", "b", "c");
String joined = Joiner.on(",").join(list); // 输出: "a,b,c"
•   Maven 依赖:
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>32.0.1-jre</version>
</dependency>
  1. JSON 处理
    1. Jackson
      • 高效的 JSON 解析和生成工具。
      • 例子:
ObjectMapper objectMapper = new ObjectMapper();
String json = objectMapper.writeValueAsString(new User("Alice", 25));
User user = objectMapper.readValue(json, User.class);
•   Maven 依赖:
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.15.2</version>
</dependency>
2.  Gson
•   Google 提供的轻量级 JSON 库。
•   例子:
Gson gson = new Gson();
String json = gson.toJson(new User("Bob", 30));
User user = gson.fromJson(json, User.class);
•   Maven 依赖:
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.10.1</version>
</dependency>

3. 日期与时间处理

1.  Joda-Time
•   Java 8 之前推荐的日期时间处理工具,Java 8 以后建议直接用 java.time 包。
•   Maven 依赖:
<dependency>
    <groupId>joda-time</groupId>
    <artifactId>joda-time</artifactId>
    <version>2.12.5</version>
</dependency>
2.  Java 8+ 原生时间工具
•   使用 java.time.LocalDate, LocalDateTime 等。
•   例子:
LocalDateTime now = LocalDateTime.now();
String formattedDate = now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

4. 文件与 IO 操作

1.  Apache Commons IO
•   专门处理文件和流操作。
•   例子:
String content = FileUtils.readFileToString(new File("example.txt"), StandardCharsets.UTF_8);
FileUtils.writeStringToFile(new File("output.txt"), "Hello, World!", StandardCharsets.UTF_8);

Maven 依赖:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.13.0</version>
</dependency>
  1. NIO Utils (Java 原生)
    • 使用 Files 类方便处理文件操作:
Path path = Paths.get("example.txt");
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
Files.write(path, "New Content".getBytes(StandardCharsets.UTF_8));

5. 集合与数据结构

1.  Apache Commons Collections
•   提供增强型集合工具类,如 Bag, MultiMap, Transformer 等。
•   Maven 依赖:
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-collections4</artifactId>
    <version>4.4</version>
</dependency>
2.  Google Guava
•   特别适合操作复杂集合结构(如 Multimap, BiMap, ImmutableList)。
•   例子:
Multimap<String, String> multimap = ArrayListMultimap.create();
multimap.put("key1", "value1");
multimap.put("key1", "value2");

6. 加密与安全

1.  Bouncy Castle
•   支持多种加密算法(如 AES, RSA, SHA)。
•   Maven 依赖:
<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk15on</artifactId>
    <version>1.76</version>
</dependency>
2.  Java 原生加密工具(JCA/JCE)
•   示例:
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] hash = md.digest("example".getBytes(StandardCharsets.UTF_8));

7. 其他

1.  Hutool
•   国人开发的 Java 工具包,功能全面(包括 JSON、日期、文件、加密等)。
•   例子:
String result = StrUtil.isEmpty("test") ? "Empty" : "Not Empty";
String date = DateUtil.now();
•   Maven 依赖:
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.8.22</version>
</dependency>

总结
• 通用工具:Apache Commons Lang、Guava、Hutool。
• JSON 处理:Jackson、Gson。
• 时间工具:Java 8 时间类或 Joda-Time。
• 文件与 IO:Apache Commons IO、Java NIO。

这些工具类可以帮助快速开发从而节省时间。

上一篇 下一篇

猜你喜欢

热点阅读