当前位置: 代码迷 >> 综合 >> hutool 工具实战
  详细解决方案

hutool 工具实战

热度:12   发布时间:2023-12-27 13:24:22.0

1.

  官网:

https://hutool.cn/docs/#/core/工具类/正则工具-ReUtil
@Testpublic void BeanToMapTest() {Log log = new Log();log.setName("hh");log.setLastValue("lastvalue");// 用反射创建一个对象并复制属性值LogVo logVo = BeanUtil.toBean(log, LogVo.class);System.out.println(logVo);System.out.println(logVo);// 复制属性值给  beanLogVo logVoCopyProperties = new  LogVo();BeanUtil.copyProperties(log,logVoCopyProperties);System.out.println(logVoCopyProperties);System.out.println(logVoCopyProperties);// 复制属性值给  mapMap<String, String> map = MapUtil.newHashMap();BeanUtil.copyProperties(log,map);System.out.println(map);}@Testpublic void CovertTest() {// Object[] a = {"a", "你", "好", "", 1};Object[] a = null ;// 将数组转集合 (需要 判断 空)//List<?> list01 = Arrays.asList(a);// 不需要 判断 null ,如果是 null 返回的 是 nullList<?> list0 = Convert.convert(List.class, a);//从4.1.11开始可以这么用 (内部有非空判断)List<?> list1 = Convert.toList(a);// 转成另一个集合类List<A> list1 = mapper.selectList(queryWrapper);List<B> applicationInfoVOList = Convert.convert(new cn.hutool.core.lang.TypeReference<List<ApplicationVO>>(){} , list1 );// 字符串 转  LocalDateTimeLocalDateTime localDateTime1 = Convert.toLocalDateTime("2020-08-14");LocalDate localDate = localDateTime1.toLocalDate();System.out.println(localDateTime1);System.out.println(localDate);}@Testpublic void MoneyTest() {// 金额大小写转换//结果为:"陆万柒仟伍佰伍拾陆元叁角贰分"String digitUppercase1 = Convert.digitToChinese(BigDecimal.valueOf(67556.00d));String digitUppercase2 = Convert.digitToChinese(BigDecimal.valueOf(67556.01d));System.out.println(digitUppercase1);System.out.println(digitUppercase2);}@Testpublic void IOTest() throws IOException {FileUtil.newFile("d:/test.txt");FileUtil.newFile("d:/test2.txt");// 拷贝(u盘 ,磁盘 etc)// 将 test.txt 的内容 拷贝到  test2BufferedInputStream in = FileUtil.getInputStream("d:/test.txt");BufferedOutputStream out = FileUtil.getOutputStream("d:/test2.txt");// 最底层是个 while 循环 ,读一部分,写入到 另一个地方,此方法也不需要 进行关闭流的操作,因为已经封装了// 底层代码://  while((readSize = in.read(buffer)) != -1) {//                out.write(buffer, 0, readSize);//                size += (long)readSize;//                out.flush();//                if (null != streamProgress) {//                    streamProgress.progress(size);//                }//            }long copySize = IoUtil.copy(in, out, DEFAULT_BUFFER_SIZE);// copy(InputStream in, OutputStream out)// 用工具包// BufferedOutputStream out2 = FileUtil.getOutputStream("d:/test2.txt");// 自己 new 一个输出流对象BufferedOutputStream out2 =  new BufferedOutputStream(new FileOutputStream(new  File("d:/test2.txt")));IoUtil.copy(new ByteArrayInputStream("hhhhhhhhhhhhhhhhhh".getBytes()),out2,DEFAULT_BUFFER_SIZE);// 可以正常写入BufferedOutputStream out3 =  new BufferedOutputStream(new FileOutputStream(new  File("d:/test2.txt")));IoUtil.copy(new ByteArrayInputStream("您好a!".getBytes()),out3,DEFAULT_BUFFER_SIZE);// 汉字OutputStreamWriter out4 =  new OutputStreamWriter(new FileOutputStream(new  File("d:/test2.txt")),CharsetUtil.CHARSET_UTF_8);long copy = IoUtil.copy(new InputStreamReader(new ByteArrayInputStream("您好!".getBytes(CharsetUtil.CHARSET_UTF_8))), out4, DEFAULT_BUFFER_SIZE);// method// byte[] readBytes(InputStream in)// 说明:读取一个 流()// 不使用工具,自己写// ① 获取输入流ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream("您好a!".getBytes());// ② 创建缓存字节数组,用来存放 从流读取的内容 (假设这个区域很大)byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];// ③ 读取流 并将内容 放到 buffer 字节数组缓存里int read = byteArrayInputStream.read(buffer);String result = new String(buffer);System.out.println(result);System.out.println(result);// 上述方法的缺点: 如果读取的内容过大 会有数据丢失情况// 使用工具ByteArrayInputStream byteArrayInputStream3 = new ByteArrayInputStream("您好a!".getBytes());
//        底层 实现:
//        byte[] buffer = new byte[bufferSize];
//        if (null != streamProgress) {
//            streamProgress.start();
//        }
//
//        long size = 0L;
//
//        int readSize;
//        try {
//            while((readSize = in.read(buffer)) != -1) {
//                out.write(buffer, 0, readSize);
//                size += (long)readSize;
//                out.flush();
//                if (null != streamProgress) {
//                    streamProgress.progress(size);
//                }
//            }
//        } catch (IOException var8) {
//            throw new IORuntimeException(var8);
//        }// 然后  out.toByteArray()byte[] bytes = IoUtil.readBytes(byteArrayInputStream3);String result2 = new String(bytes);System.out.println(result2);}@Testpublic void StringTest() throws IOException{String template = "{}爱{},就像老鼠爱大米";String str = StrUtil.format(template, "我", "你"); //str -> 我爱你,就像老鼠爱大米System.out.println(str);}

对象的赋值用spring原生的:

 BeanUtils.copyProperties(vo, info);

一个集合转成另一个集合并赋值:

  List<ApplicationInfoVersion> applicationInfoVersionsFromDb = applicationInfoVersionMapper.selectList(versionQueryWrapper);List<ApplicationInfoVersionVO> versionVOList = Convert.convert(new cn.hutool.core.lang.TypeReference<List<ApplicationInfoVersionVO>>() {}, applicationInfoVersionsFromDb);