mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-06-03 04:24:05 +08:00
[update] FileUtil增加读取大文件自定义方法和单纯读取方法
This commit is contained in:
parent
59f0e6e296
commit
70aad08b97
@ -1,6 +1,14 @@
|
||||
yexuejc-base 更新记录
|
||||
------------------
|
||||
|
||||
#### version :1.5.3-jre11
|
||||
**time: ** <br/>
|
||||
**branch:** jre11 <br/>
|
||||
**update:** <br/>
|
||||
1. [FileUtil.java](src/main/java/com/yexuejc/base/util/FileUtil.java)增加读取大文件自定义方法和单纯读取方法
|
||||
2.
|
||||
---
|
||||
|
||||
#### version :1.5.2-jre11
|
||||
**time:2024-4-7 14:34:33** <br/>
|
||||
**branch:** jre11 <br/>
|
||||
|
@ -18,6 +18,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.zip.CRC32;
|
||||
@ -38,11 +39,14 @@ import io.jsonwebtoken.lang.Assert;
|
||||
* @time 2017年11月3日 下午3:12:49
|
||||
*/
|
||||
public class FileUtil {
|
||||
static Logger logger = Logger.getLogger(FileUtil.class.getName());
|
||||
private static final Logger logger = Logger.getLogger(FileUtil.class.getName());
|
||||
|
||||
private FileUtil() {
|
||||
}
|
||||
|
||||
|
||||
private static final String NEW_LINE = "\n";
|
||||
private static final String CONSTANT_DOT = ".";
|
||||
private static final String TYPE_TAR_GZ = ".tar.gz";
|
||||
private static final String TYPE_CSV = ".csv";
|
||||
private static final String TAR_GZ = "tar.gz";
|
||||
@ -61,7 +65,7 @@ public class FileUtil {
|
||||
if (fileName.lastIndexOf(TYPE_TAR_GZ) > 0) {
|
||||
return TAR_GZ;
|
||||
}
|
||||
return fileName.substring(fileName.lastIndexOf(".") + 1);
|
||||
return fileName.substring(fileName.lastIndexOf(CONSTANT_DOT) + 1);
|
||||
} catch (Exception e) {
|
||||
logger.severe("file doesn't exist or is not a file");
|
||||
}
|
||||
@ -332,22 +336,19 @@ public class FileUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 分段读取大文件
|
||||
* 分段读取大文件(不限格式)
|
||||
*
|
||||
* @param csvFilePath 文件路径
|
||||
* @param filePath 文件路径
|
||||
* @param readFileBean 分段每次读取的bean 初始值需要设置每次读取的行数
|
||||
* @param <T> 读取结果类型bean
|
||||
* @return
|
||||
* @return 文件分页读取内容(自定义处理后)及读取信息
|
||||
*/
|
||||
public static <T> ReadFileBean<T> readBigFile(String csvFilePath, ReadFileBean<T> readFileBean, Class<T> readCls) throws IOException {
|
||||
if (!isFileExist(csvFilePath)) {
|
||||
throw new FileNotFoundException(String.format("解析用的csv: [%s] 文件不存在。", csvFilePath));
|
||||
}
|
||||
if (!csvFilePath.endsWith(TYPE_CSV)) {
|
||||
throw new IOException(String.format("解析用的csv: [%s] 文件不是CSV文件格式。", csvFilePath));
|
||||
public static <T> ReadFileBean<T> readBigFile(String filePath, ReadFileBean<T> readFileBean, Function<List<String>, List<T>> readAfter) throws IOException {
|
||||
if (!isFileExist(filePath)) {
|
||||
throw new FileNotFoundException(String.format("[%s]文件不存在。", filePath));
|
||||
}
|
||||
List<String> datas = new ArrayList<>();
|
||||
try (RandomAccessFile randomAccessFile = new RandomAccessFile(new File(csvFilePath), "r")) {
|
||||
try (RandomAccessFile randomAccessFile = new RandomAccessFile(new File(filePath), "r")) {
|
||||
if (readFileBean.getPointer() < 0) {
|
||||
readFileBean.setPointer(0);
|
||||
}
|
||||
@ -365,29 +366,60 @@ public class FileUtil {
|
||||
//无数据
|
||||
return readFileBean.setDatas(new ArrayList<>());
|
||||
}
|
||||
|
||||
//csv文件处理
|
||||
com.yexuejc.base.pojo.CsvToBean csvToBean = getCsvToBean(readCls);
|
||||
readFileBean.setHeader(csvToBean.getHeader());
|
||||
if (csvToBean.hasHeader()) {
|
||||
//文件存在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());
|
||||
List<T> dataList = readAfter.apply(datas);
|
||||
readFileBean.setDatas(dataList);
|
||||
return readFileBean;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分段读取大文件(不解析)
|
||||
*
|
||||
* @param csvFilePath 文件路径
|
||||
* @param readFileBean 分段每次读取的bean 初始值需要设置每次读取的行数
|
||||
* @return 文件分页读取内容(每行为一个String对象)及读取信息
|
||||
*/
|
||||
public static ReadFileBean<String> readBigFile(String csvFilePath, ReadFileBean<String> readFileBean) throws IOException {
|
||||
return readBigFile(csvFilePath, readFileBean, (datas) -> datas);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分段读取大文件(CSV格式)
|
||||
*
|
||||
* @param csvFilePath 文件路径
|
||||
* @param readFileBean 分段每次读取的bean 初始值需要设置每次读取的行数
|
||||
* @param <T> 读取结果类型bean
|
||||
* @return 文件分页读取内容(转bean后)及读取信息
|
||||
*/
|
||||
public static <T> ReadFileBean<T> readBigFile(String csvFilePath, ReadFileBean<T> readFileBean, Class<T> readCls) throws IOException {
|
||||
if (!csvFilePath.endsWith(TYPE_CSV)) {
|
||||
throw new IOException(String.format("[%s]文件不是CSV文件格式。", csvFilePath));
|
||||
}
|
||||
return readBigFile(csvFilePath, readFileBean, (datas) -> {
|
||||
//csv文件处理
|
||||
com.yexuejc.base.pojo.CsvToBean csvToBean = getCsvToBean(readCls);
|
||||
readFileBean.setHeader(csvToBean.getHeader());
|
||||
if (csvToBean.hasHeader()) {
|
||||
//文件存在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());
|
||||
}
|
||||
try {
|
||||
return readCsv(String.join(NEW_LINE, datas), readCls, csvToBean.getDelimiter());
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取csv的header,使用注解{@link CsvToBean}
|
||||
*
|
||||
|
Loading…
x
Reference in New Issue
Block a user