java中jar工具包hutool的作用

[复制链接]
admin 发表于 2025-8-21 11:05:04 | 显示全部楼层 |阅读模式
java中jar工具包hutool的作用

Hutool 简介
Hutool 是一个功能丰富且易用的Java工具库,涵盖了字符串处理、日期时间操作、文件操作、加密解密、HTTP请求、JSON处理等各个方面。它的设计目标是尽量减少开发人员在项目中编写重复代码的工作量。
主要功能模块
hutool-core - 核心模块,包含字符串、日期、集合等基础工具
hutool-json - JSON处理工具
hutool-http - HTTP客户端工具
hutool-crypto - 加密解密工具
hutool-setting - 配置文件工具
hutool-cache - 缓存工具
hutool-db - 数据库工具
  1. <dependency>
  2.     <groupId>cn.hutool</groupId>
  3.     <artifactId>hutool-all</artifactId>
  4.     <version>5.8.27</version>
  5. </dependency>
复制代码
案例:
  1. import cn.hutool.core.io.IoUtil;
  2. import cn.hutool.json.JSONConfig;
  3. import cn.hutool.json.JSONUtil;
  4. import com.itheima.pojo.User;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import java.io.InputStream;
  8. import java.nio.charset.StandardCharsets;
  9. import java.time.LocalDateTime;
  10. import java.time.format.DateTimeFormatter;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.stream.Collectors;

  14. @RestController
  15. public class UserController {
  16.    
  17.     @RequestMapping("/list")
  18.     public String list(){
  19.         //1.加载并读取文件
  20.         InputStream in = this.getClass().getClassLoader().getResourceAsStream("user.txt");
  21.         ArrayList<String> lines = IoUtil.readLines(in, StandardCharsets.UTF_8, new ArrayList<>());
  22.         
  23.         //2.解析数据,封装成对象 --> 集合
  24.         List<User> userList = lines.stream().map(line -> {
  25.             String[] parts = line.split(",");
  26.             Integer id = Integer.parseInt(parts[0]);
  27.             String username = parts[1];
  28.             String password = parts[2];
  29.             String name = parts[3];
  30.             Integer age = Integer.parseInt(parts[4]);
  31.             LocalDateTime updateTime = LocalDateTime.parse(parts[5], DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

  32.             return new User(id, username, password, name, age, updateTime);
  33.         }).collect(Collectors.toList());
  34.         
  35.         //3.响应数据
  36.         //return JSONUtil.toJsonStr(userList, JSONConfig.create().setDateFormat("yyyy-MM-dd HH:mm:ss"));
  37.         return userList;
  38.     }
  39.    
  40. }
复制代码


网站建设,公众号小程序开发,多商户单商户小程序制作,高端系统定制开发,App软件开发联系我们【手机/微信:17817817816
微信扫码

网站建设,公众号小程序开发,商城小程序,系统定制开发,App软件开发等

粤ICP备2024252464号

在本版发帖
微信扫码
QQ客服返回顶部
快速回复 返回顶部 返回列表