mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-06-06 13:54:03 +08:00
1.5.1-jre11
This commit is contained in:
parent
3ddd11f9c9
commit
0dc13a6aa1
14
UPDATE.md
14
UPDATE.md
@ -1,6 +1,20 @@
|
|||||||
yexuejc-base 更新记录
|
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
|
#### version :1.5.0-jre8
|
||||||
**time:2022-5-9 13:37:31** <br/>
|
**time:2022-5-9 13:37:31** <br/>
|
||||||
**branch:** master <br/>
|
**branch:** master <br/>
|
||||||
|
2
pom.xml
2
pom.xml
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
<groupId>top.yexuejc</groupId>
|
<groupId>top.yexuejc</groupId>
|
||||||
<artifactId>yexuejc-base</artifactId>
|
<artifactId>yexuejc-base</artifactId>
|
||||||
<version>1.5.2-jdk11</version>
|
<version>1.5.1-jre11</version>
|
||||||
|
|
||||||
<name>yexuejc-base</name>
|
<name>yexuejc-base</name>
|
||||||
<url>https://github.com/yexuejc/yexuejc-base</url>
|
<url>https://github.com/yexuejc/yexuejc-base</url>
|
||||||
|
@ -11,11 +11,21 @@ import java.lang.annotation.*;
|
|||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Documented
|
@Documented
|
||||||
@Inherited
|
@Inherited
|
||||||
public @interface CsvHeader {
|
public @interface CsvToBean {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 在类头上设置csv格式的header
|
* 在类头上设置csv格式的header
|
||||||
* @return
|
* csv文件和java bean的映射
|
||||||
*/
|
*/
|
||||||
String header();
|
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;
|
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.List;
|
||||||
|
import java.util.function.Function;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author maxf
|
* @author maxf
|
||||||
@ -34,7 +39,18 @@ public class ReadFileBean<T> {
|
|||||||
* 文件的length
|
* 文件的length
|
||||||
*/
|
*/
|
||||||
private long fileLength;
|
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) {
|
public ReadFileBean(int readRow) {
|
||||||
this.readRowNum = readRow;
|
this.readRowNum = readRow;
|
||||||
@ -42,6 +58,24 @@ public class ReadFileBean<T> {
|
|||||||
this.endRowNum = 0;
|
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() {
|
public int getReadRowNum() {
|
||||||
return readRowNum;
|
return readRowNum;
|
||||||
}
|
}
|
||||||
@ -82,4 +116,8 @@ public class ReadFileBean<T> {
|
|||||||
this.fileLength = fileLength;
|
this.fileLength = fileLength;
|
||||||
return this;
|
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.fasterxml.jackson.dataformat.csv.CsvSchema;
|
||||||
import com.google.common.io.ByteStreams;
|
import com.google.common.io.ByteStreams;
|
||||||
import com.google.common.io.CharStreams;
|
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 com.yexuejc.base.pojo.ReadFileBean;
|
||||||
import io.jsonwebtoken.lang.Assert;
|
import io.jsonwebtoken.lang.Assert;
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
@ -28,7 +28,6 @@ import java.math.BigInteger;
|
|||||||
import java.nio.MappedByteBuffer;
|
import java.nio.MappedByteBuffer;
|
||||||
import java.nio.channels.FileChannel;
|
import java.nio.channels.FileChannel;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
@ -52,7 +51,7 @@ import java.util.zip.CRC32;
|
|||||||
public class FileUtil {
|
public class FileUtil {
|
||||||
static Logger logger = Logger.getLogger(FileUtil.class.getName());
|
static Logger logger = Logger.getLogger(FileUtil.class.getName());
|
||||||
|
|
||||||
private FileUtil() {
|
public FileUtil() {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static final String TYPE_TAR_GZ = ".tar.gz";
|
private static final String TYPE_TAR_GZ = ".tar.gz";
|
||||||
@ -491,24 +490,48 @@ public class FileUtil {
|
|||||||
* @param <T> 读取结果类型bean
|
* @param <T> 读取结果类型bean
|
||||||
* @return
|
* @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);
|
File file = new File(path);
|
||||||
judeFileExists(file);
|
if (!file.exists() || file.isDirectory()) {
|
||||||
try {
|
throw new FileNotFoundException("file:" + path + " is not found.");
|
||||||
RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r");
|
}
|
||||||
|
try (RandomAccessFile randomAccessFile = new RandomAccessFile(file, "r")) {
|
||||||
|
if (readFileBean.getPointer() < 0) {
|
||||||
|
readFileBean.setPointer(0);
|
||||||
|
}
|
||||||
randomAccessFile.seek(readFileBean.getPointer());
|
randomAccessFile.seek(readFileBean.getPointer());
|
||||||
readFileBean.setFileLength(randomAccessFile.length());
|
readFileBean.setFileLength(randomAccessFile.length());
|
||||||
List<String> datas = new ArrayList<>();
|
List<String> datas = new ArrayList<>();
|
||||||
for (int i = 0; i < readFileBean.getReadRowNum(); i++) {
|
int row = 1;
|
||||||
String s = randomAccessFile.readLine();
|
String line;
|
||||||
datas.add(charsetDecode(s, StandardCharsets.UTF_8));
|
while ((line = randomAccessFile.readLine()) != null && row < readFileBean.getReadRowNum()) {
|
||||||
|
row++;
|
||||||
readFileBean.setPointer(randomAccessFile.getFilePointer());
|
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)) {
|
if (path.contains(TYPE_CSV)) {
|
||||||
//csv文件处理
|
//csv文件处理
|
||||||
datas.add(0, getCsvHeader(readCls));
|
com.yexuejc.base.pojo.CsvToBean csvToBean = getCsvToBean(readCls);
|
||||||
List<T> dataList = readCsv(String.join("\n", datas), 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);
|
readFileBean.setDatas(dataList);
|
||||||
}
|
}
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
@ -520,18 +543,18 @@ public class FileUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取csv的header,使用注解{@link CsvHeader}
|
* 获取csv的header,使用注解{@link CsvToBean}
|
||||||
*
|
*
|
||||||
* @param cls
|
* @param cls
|
||||||
* @param <T>
|
* @param <T>
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static <T> String getCsvHeader(Class<T> cls) {
|
public static <T> com.yexuejc.base.pojo.CsvToBean getCsvToBean(Class<T> cls) {
|
||||||
CsvHeader annotation = cls.getAnnotation(CsvHeader.class);
|
CsvToBean annotation = cls.getAnnotation(CsvToBean.class);
|
||||||
Assert.notNull(annotation, cls.toString() + "类上需要添加注解@CsvHeader,并指定header。");
|
Assert.notNull(annotation, cls.toString() + "类上需要添加注解@CsvToBean,并指定header。");
|
||||||
String header = annotation.header();
|
com.yexuejc.base.pojo.CsvToBean csvToBean = new com.yexuejc.base.pojo.CsvToBean(
|
||||||
Assert.notNull(header, cls.toString() + "类上需要添加注解@CsvHeader,并指定header。");
|
annotation.header(), annotation.delimiter(), annotation.hasHeader());
|
||||||
return header;
|
return csvToBean;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
x
Reference in New Issue
Block a user