mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-06-06 13:54:03 +08:00
Merge remote-tracking branch 'origin/jre11' into jre11
This commit is contained in:
commit
a260b41fae
14
UPDATE.md
14
UPDATE.md
@ -1,6 +1,20 @@
|
||||
yexuejc-base 更新记录
|
||||
------------------
|
||||
|
||||
#### version :1.5.1-jre11
|
||||
**time:2023-6-8 16:02:56** <br/>
|
||||
**branch:** jre11 <br/>
|
||||
**update:** <br/>
|
||||
1. FileUtil 增加读取csv文件(分页读取)
|
||||
提供读取IO流方法合集
|
||||
2. 增加AES加解密
|
||||
3. 增加文件压缩ZipUtil
|
||||
4. DateUtil,DateTimeUtil 时间操作工具优化
|
||||
5. JsonUtil [feat] 增加json化时对LocalDateTime,LocalDate,Timestamp时间的优化,增加特殊场景序列化反序列化
|
||||
|
||||
|
||||
|
||||
---
|
||||
#### version :1.5.0-jre8
|
||||
**time:2022-5-9 13:37:31** <br/>
|
||||
**branch:** master <br/>
|
||||
|
2
pom.xml
2
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>top.yexuejc</groupId>
|
||||
<artifactId>yexuejc-base</artifactId>
|
||||
<version>1.5.2-jdk11</version>
|
||||
<version>1.5.1-jre11</version>
|
||||
|
||||
<name>yexuejc-base</name>
|
||||
<url>https://github.com/yexuejc/yexuejc-base</url>
|
||||
|
@ -11,11 +11,21 @@ import java.lang.annotation.*;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
@Inherited
|
||||
public @interface CsvHeader {
|
||||
public @interface CsvToBean {
|
||||
|
||||
/**
|
||||
* 在类头上设置csv格式的header
|
||||
* @return
|
||||
* csv文件和java bean的映射
|
||||
*/
|
||||
String header();
|
||||
|
||||
/**
|
||||
* csv文件的分割符号
|
||||
*/
|
||||
char delimiter() default ',';
|
||||
|
||||
/**
|
||||
* CSV文件是否有header
|
||||
*/
|
||||
boolean hasHeader() default false;
|
||||
}
|
61
src/main/java/com/yexuejc/base/pojo/CsvToBean.java
Normal file
61
src/main/java/com/yexuejc/base/pojo/CsvToBean.java
Normal file
@ -0,0 +1,61 @@
|
||||
package com.yexuejc.base.pojo;
|
||||
|
||||
/**
|
||||
* {@link com.yexuejc.base.annotation.CsvToBean}
|
||||
* 读取csv文件用
|
||||
* @author yexuejc
|
||||
* @class-name CsvToBean
|
||||
* @description
|
||||
* @date 2023/6/6 18:15
|
||||
*/
|
||||
public class CsvToBean {
|
||||
|
||||
/**
|
||||
* 在类头上设置csv格式的header
|
||||
* csv文件和java bean的映射
|
||||
*/
|
||||
private String header;
|
||||
|
||||
/**
|
||||
* csv文件的分割符号
|
||||
*/
|
||||
private char delimiter;
|
||||
|
||||
/**
|
||||
* CSV文件是否有header
|
||||
*/
|
||||
private boolean hasHeader;
|
||||
|
||||
public CsvToBean(String header, char delimiter, boolean hasHeader) {
|
||||
this.header = header;
|
||||
this.delimiter = delimiter;
|
||||
this.hasHeader = hasHeader;
|
||||
}
|
||||
|
||||
public String getHeader() {
|
||||
return header;
|
||||
}
|
||||
|
||||
public CsvToBean setHeader(String header) {
|
||||
this.header = header;
|
||||
return this;
|
||||
}
|
||||
|
||||
public char getDelimiter() {
|
||||
return delimiter;
|
||||
}
|
||||
|
||||
public CsvToBean setDelimiter(char delimiter) {
|
||||
this.delimiter = delimiter;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isHasHeader() {
|
||||
return hasHeader;
|
||||
}
|
||||
|
||||
public CsvToBean setHasHeader(boolean hasHeader) {
|
||||
this.hasHeader = hasHeader;
|
||||
return this;
|
||||
}
|
||||
}
|
@ -1,6 +1,11 @@
|
||||
package com.yexuejc.base.pojo;
|
||||
|
||||
import org.checkerframework.checker.units.qual.C;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
/**
|
||||
* @author maxf
|
||||
@ -34,7 +39,18 @@ public class ReadFileBean<T> {
|
||||
* 文件的length
|
||||
*/
|
||||
private long fileLength;
|
||||
/**
|
||||
* csv文件存在header
|
||||
*/
|
||||
private String header;
|
||||
private Charset readCharset = StandardCharsets.UTF_8;
|
||||
public Function<String, String> lineScavenger = t -> t;
|
||||
|
||||
public ReadFileBean() {
|
||||
this.readRowNum = Integer.MAX_VALUE;
|
||||
this.startRowNum = 1;
|
||||
this.endRowNum = 0;
|
||||
}
|
||||
|
||||
public ReadFileBean(int readRow) {
|
||||
this.readRowNum = readRow;
|
||||
@ -42,6 +58,24 @@ public class ReadFileBean<T> {
|
||||
this.endRowNum = 0;
|
||||
}
|
||||
|
||||
public Charset getReadCharset() {
|
||||
return readCharset;
|
||||
}
|
||||
|
||||
public ReadFileBean<T> setReadCharset(Charset readCharset) {
|
||||
this.readCharset = readCharset;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getHeader() {
|
||||
return header;
|
||||
}
|
||||
|
||||
public ReadFileBean<T> setHeader(String header) {
|
||||
this.header = header;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getReadRowNum() {
|
||||
return readRowNum;
|
||||
}
|
||||
@ -82,4 +116,8 @@ public class ReadFileBean<T> {
|
||||
this.fileLength = fileLength;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String lineScavenge(String lineData) {
|
||||
return this.lineScavenger.apply(lineData);
|
||||
}
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ import com.fasterxml.jackson.dataformat.csv.CsvMapper;
|
||||
import com.fasterxml.jackson.dataformat.csv.CsvSchema;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.google.common.io.CharStreams;
|
||||
import com.yexuejc.base.annotation.CsvHeader;
|
||||
import com.yexuejc.base.annotation.CsvToBean;
|
||||
import com.yexuejc.base.pojo.ReadFileBean;
|
||||
import io.jsonwebtoken.lang.Assert;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
@ -28,7 +28,6 @@ import java.math.BigInteger;
|
||||
import java.nio.MappedByteBuffer;
|
||||
import java.nio.channels.FileChannel;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.MessageDigest;
|
||||
@ -52,7 +51,7 @@ import java.util.zip.CRC32;
|
||||
public class FileUtil {
|
||||
static Logger logger = Logger.getLogger(FileUtil.class.getName());
|
||||
|
||||
private FileUtil() {
|
||||
public FileUtil() {
|
||||
}
|
||||
|
||||
private static final String TYPE_TAR_GZ = ".tar.gz";
|
||||
@ -491,24 +490,48 @@ public class FileUtil {
|
||||
* @param <T> 读取结果类型bean
|
||||
* @return
|
||||
*/
|
||||
public <T> ReadFileBean<T> readBigFile(String path, ReadFileBean<T> readFileBean, Class<T> readCls) {
|
||||
public <T> ReadFileBean<T> readBigFile(String path, ReadFileBean<T> readFileBean, Class<T> readCls) throws FileNotFoundException {
|
||||
File file = new File(path);
|
||||
judeFileExists(file);
|
||||
try {
|
||||
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
|
||||
if (!file.exists() || file.isDirectory()) {
|
||||
throw new FileNotFoundException("file:" + path + " is not found.");
|
||||
}
|
||||
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
|
||||
if (readFileBean.getPointer() < 0) {
|
||||
readFileBean.setPointer(0);
|
||||
}
|
||||
randomAccessFile.seek(readFileBean.getPointer());
|
||||
readFileBean.setFileLength(randomAccessFile.length());
|
||||
List<String> datas = new ArrayList<>();
|
||||
for (int i = 0; i < readFileBean.getReadRowNum(); i++) {
|
||||
String s = randomAccessFile.readLine();
|
||||
datas.add(charsetDecode(s, StandardCharsets.UTF_8));
|
||||
int row = 1;
|
||||
String line;
|
||||
while ((line = randomAccessFile.readLine()) != null && row < readFileBean.getReadRowNum()) {
|
||||
row++;
|
||||
readFileBean.setPointer(randomAccessFile.getFilePointer());
|
||||
datas.add(readFileBean.lineScavenge(charsetDecode(line, readFileBean.getReadCharset()))); }
|
||||
|
||||
if (StrUtil.isEmpty(datas)) {
|
||||
//无数据
|
||||
return readFileBean.setDatas(List.of());
|
||||
}
|
||||
randomAccessFile.close();
|
||||
if (path.contains(TYPE_CSV)) {
|
||||
//csv文件处理
|
||||
datas.add(0, getCsvHeader(readCls));
|
||||
List<T> dataList = readCsv(String.join("\n", datas), readCls, ',');
|
||||
com.yexuejc.base.pojo.CsvToBean csvToBean = getCsvToBean(readCls);
|
||||
readFileBean.setHeader(csvToBean.getHeader());
|
||||
if (csvToBean.isHasHeader()) {
|
||||
//文件存在header,设置header优先,没设置使用文件的
|
||||
if (StrUtil.isNotEmpty(csvToBean.getHeader())) {
|
||||
//替换header
|
||||
datas.remove(0);
|
||||
datas.add(0, csvToBean.getHeader());
|
||||
} else {
|
||||
readFileBean.setHeader(datas.get(0));
|
||||
}
|
||||
} else {
|
||||
//文件不存在header,使用设置的
|
||||
datas.add(0, csvToBean.getHeader());
|
||||
}
|
||||
|
||||
List<T> dataList = readCsv(String.join("\n", datas), readCls, csvToBean.getDelimiter());
|
||||
readFileBean.setDatas(dataList);
|
||||
}
|
||||
} catch (FileNotFoundException e) {
|
||||
@ -520,18 +543,18 @@ public class FileUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取csv的header,使用注解{@link CsvHeader}
|
||||
* 获取csv的header,使用注解{@link CsvToBean}
|
||||
*
|
||||
* @param cls
|
||||
* @param <T>
|
||||
* @return
|
||||
*/
|
||||
public static <T> String getCsvHeader(Class<T> cls) {
|
||||
CsvHeader annotation = cls.getAnnotation(CsvHeader.class);
|
||||
Assert.notNull(annotation, cls.toString() + "类上需要添加注解@CsvHeader,并指定header。");
|
||||
String header = annotation.header();
|
||||
Assert.notNull(header, cls.toString() + "类上需要添加注解@CsvHeader,并指定header。");
|
||||
return header;
|
||||
public static <T> com.yexuejc.base.pojo.CsvToBean getCsvToBean(Class<T> cls) {
|
||||
CsvToBean annotation = cls.getAnnotation(CsvToBean.class);
|
||||
Assert.notNull(annotation, cls.toString() + "类上需要添加注解@CsvToBean,并指定header。");
|
||||
com.yexuejc.base.pojo.CsvToBean csvToBean = new com.yexuejc.base.pojo.CsvToBean(
|
||||
annotation.header(), annotation.delimiter(), annotation.hasHeader());
|
||||
return csvToBean;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user