1.
官网:
https://hutool.cn/docs/#/core/工具类/正则工具-ReUtil
@Test
public 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);
// 复制属性值给 bean
LogVo logVoCopyProperties = new LogVo();
BeanUtil.copyProperties(log,logVoCopyProperties);
System.out.println(logVoCopyProperties);
System.out.println(logVoCopyProperties);
// 复制属性值给 map
Map<String, String> map = MapUtil.newHashMap();
BeanUtil.copyProperties(log,map);
System.out.println(map);
}
@Test
public void CovertTest() {
// Object[] a = {"a", "你", "好", "", 1};
Object[] a = null ;
// 将数组转集合 (需要 判断 空)
//List<?> list01 = Arrays.asList(a);
// 不需要 判断 null ,如果是 null 返回的 是 null
List<?> 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 );
// 字符串 转 LocalDateTime
LocalDateTime localDateTime1 = Convert.toLocalDateTime("2020-08-14");
LocalDate localDate = localDateTime1.toLocalDate();
System.out.println(localDateTime1);
System.out.println(localDate);
}
@Test
public 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);
}
@Test
public void IOTest() throws IOException {
FileUtil.newFile("d:/test.txt");
FileUtil.newFile("d:/test2.txt");
// 拷贝(u盘 ,磁盘 etc)
// 将 test.txt 的内容 拷贝到 test2
BufferedInputStream 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);
}
@Test
public 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);
1. 内存分页
/***
* 内存分页
*
* @param dataList 待分页的数据
* @param pageIndex 第几页
* @param pageSize 每页获取多少内容
* @author 徐鹏军
* @date 2022/4/14 13:20
* @return {@link List<T>}
*/
public static <T> List<T> page(List<T> dataList, int pageIndex, int pageSize) {
if (CollUtil.isEmpty(dataList)) {
return Collections.emptyList();
}
int[] trans = PageUtil.transToStartEnd(pageIndex, pageSize);
return CollUtil.sub(dataList, trans[0], trans[1]);
}