一、Hutool介绍
Hutool是一个Java工具类库,提供了丰富的实用方法来简化Java开发。
以下是Hutool的主要功能:
字符串处理:提供多种字符串操作方法,如格式化、截取、替换等。
日期时间处理:简化日期和时间的解析、格式化和计算。
集合操作:增强对Java集合的操作能力,如过滤、排序、分组等。
文件操作:简化文件读写、复制、移动等操作。
网络请求:封装了HTTP请求的发送和响应处理。
加密解密:支持多种加密算法,如MD5、SHA、AES等。
JSON处理:提供便捷的JSON序列化和反序列化方法。
二、Hutool常用功能例子
字符串处理:
import cn.hutool.core.util.StrUtil;
public class StringExample {
public static void main(String[] args) {
// 格式化字符串
String formatted = StrUtil.format("Hello, {}", "World");
System.out.println(formatted); // 输出: Hello, World
// 截取字符串
String subStr = StrUtil.sub("Hello, World", 0, 5);
System.out.println(subStr); // 输出: Hello
// 替换字符串
String replaced = StrUtil.replace("Hello, World", "World", "Hutool");
System.out.println(replaced); // 输出: Hello, Hutool
}
}
日期时间处理
import cn.hutool.core.date.DateUtil;
public class DateExample {
public static void main(String[] args) {
// 获取当前日期时间
String now = DateUtil.now();
System.out.println(now); // 输出: 当前日期时间,格式为 yyyy-MM-dd HH:mm:ss
// 格式化日期
String formatDate = DateUtil.format(DateUtil.date(), "yyyy-MM-dd");
System.out.println(formatDate); // 输出: 当前日期,格式为 yyyy-MM-dd
// 解析日期
java.util.Date date = DateUtil.parse("2023-10-01");
System.out.println(date); // 输出: Sun Oct 01 00:00:00 GMT 2023
}
}
集合操作
import cn.hutool.core.collection.CollUtil;
import java.util.List;
public class CollectionExample {
public static void main(String[] args) {
List<String> list = CollUtil.newArrayList("Apple", "Banana", "Cherry");
// 过滤集合
List<String> filteredList = CollUtil.filter(list, s -> s.startsWith("A"));
System.out.println(filteredList); // 输出: [Apple]
// 排序集合
CollUtil.sort(list);
System.out.println(list); // 输出: [Apple, Banana, Cherry]
// 分组集合
Map<Character, List<String>> groupedMap = CollUtil.group(list, s -> s.charAt(0));
System.out.println(groupedMap); // 输出: {A=[Apple], B=[Banana], C=[Cherry]}
}
}
文件操作
import cn.hutool.core.io.FileUtil;
public class FileExample {
public static void main(String[] args) {
// 读取文件内容
String content = FileUtil.readString("example.txt", java.nio.charset.StandardCharsets.UTF_8);
System.out.println(content); // 输出: 文件example.txt的内容
// 写入文件内容
FileUtil.writeString("Hello, Hutool!", "example.txt", java.nio.charset.StandardCharsets.UTF_8);
// 复制文件
FileUtil.copy("example.txt", "example_copy.txt", true);
// 移动文件
FileUtil.move("example_copy.txt", "moved_example.txt", true);
}
}
网络请求
import cn.hutool.http.HttpUtil;
public class HttpExample {
public static void main(String[] args) {
// 发送GET请求
String response = HttpUtil.get("https://api.github.com");
System.out.println(response); // 输出: GitHub API响应内容
// 发送POST请求
String postResponse = HttpUtil.post("https://httpbin.org/post", "name=Hutool");
System.out.println(postResponse); // 输出: POST请求响应内容
}
}
加密解密
import cn.hutool.crypto.SecureUtil;
public class CryptoExample {
public static void main(String[] args) {
// MD5加密
String md5 = SecureUtil.md5("Hello, Hutool!");
System.out.println(md5); // 输出: MD5加密后的字符串
// SHA-256加密
String sha256 = SecureUtil.sha256("Hello, Hutool!");
System.out.println(sha256); // 输出: SHA-256加密后的字符串
// AES加密
String content = "Hello, Hutool!";
String key = "1234567890123456"; // 16字节密钥
String aesEncrypt = SecureUtil.aes(key.getBytes()).encryptHex(content);
System.out.println(aesEncrypt); // 输出: AES加密后的字符串
// AES解密
String aesDecrypt = SecureUtil.aes(key.getBytes()).decryptStr(aesEncrypt);
System.out.println(aesDecrypt); // 输出: Hello, Hutool!
}
}
JSON处理
import cn.hutool.json.JSONUtil;
public class JsonExample {
public static void main(String[] args) {
// 创建JSON对象
cn.hutool.json.JSONObject jsonObject = JSONUtil.createObj()
.put("name", "Hutool")
.put("version", "5.8.11");
System.out.println(jsonObject.toString()); // 输出: {"name":"Hutool","version":"5.8.11"}
// 序列化Java对象为JSON字符串
Person person = new Person("Hutool", 5);
String jsonString = JSONUtil.toJsonStr(person);
System.out.println(jsonString); // 输出: {"age":5,"name":"Hutool"}
// 反序列化JSON字符串为Java对象
Person parsedPerson = JSONUtil.toBean(jsonString, Person.class);
System.out.println(parsedPerson.getName()); // 输出: Hutool
}
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}