mirror of
https://gitee.com/jzsw-it/yexuejc-base.git
synced 2025-06-06 22:04:04 +08:00
[update] FileUtil增加读取大文件自定义方法和单纯读取方法
This commit is contained in:
parent
70aad08b97
commit
0954eb64b5
@ -0,0 +1,38 @@
|
|||||||
|
package com.yexuejc.base.converter;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonParser;
|
||||||
|
import com.fasterxml.jackson.databind.DeserializationContext;
|
||||||
|
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 反序列化中内容为空时,返回Integer为空
|
||||||
|
* <p>使用方式:@JsonDeserialize(using = IntegerNullValueDeserializer.class)</p>
|
||||||
|
* @author: yexuejc
|
||||||
|
* @date: 2024/4/15 18:08
|
||||||
|
*/
|
||||||
|
public class IntegerNullValueDeserializer extends StdScalarDeserializer<Integer> {
|
||||||
|
public IntegerNullValueDeserializer() {
|
||||||
|
super(Integer.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Integer deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
|
||||||
|
String value = p.getValueAsString();
|
||||||
|
if (isInteger(value)) {
|
||||||
|
return super._parseInteger(p, ctxt, Integer.class);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isInteger(String s) {
|
||||||
|
try {
|
||||||
|
Integer.parseInt(s);
|
||||||
|
return true;
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,7 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import com.yexuejc.base.pojo.ReadFileBean;
|
import com.yexuejc.base.pojo.ReadFileBean;
|
||||||
import com.yexuejc.base.util.bean.AppnodeCertCsvBean;
|
import com.yexuejc.base.util.bean.AppnodeCertCsvBean;
|
||||||
@ -54,19 +55,63 @@ public class FileUtilTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void readCsvFile() throws IOException {
|
private static void readCsvFile() throws IOException {
|
||||||
String path = "F:\\coding\\yexuejc-base2\\src\\test\\java\\com\\yexuejc\\base\\util\\test.csv";
|
String path = "F:\\coding\\yexuejc-base\\src\\test\\java\\com\\yexuejc\\base\\util\\test.csv";
|
||||||
|
|
||||||
List<AppnodeCertCsvBean> list = FileUtil.readCsv(path, AppnodeCertCsvBean.class, true, "enable,domain,protocol,deployHost,deployPath,uname,pwd,appnodeId", ',');
|
// List<AppnodeCertCsvBean> list = FileUtil.readCsv(path, AppnodeCertCsvBean.class, true, "enable,domain,protocol,deployHost,deployPath,uname,pwd,appnodeId", ',');
|
||||||
System.out.println("***********************************************");
|
// System.out.println("***********************************************");
|
||||||
System.out.println(JsonUtil.formatPrinter(list));
|
// System.out.println(JsonUtil.formatPrinter(list));
|
||||||
System.out.println("条数:" + list.size());
|
// System.out.println("条数:" + list.size());
|
||||||
|
|
||||||
|
//直接把每行读取成字符串
|
||||||
|
ReadFileBean<String> readFileBean2 = new ReadFileBean<>(2);
|
||||||
|
ReadFileBean<String> bean2 = FileUtil.readBigFile(path, readFileBean2);
|
||||||
|
System.out.println("直接把每行读取成字符串============================================");
|
||||||
|
System.out.println(JsonUtil.formatPrinter(bean2));
|
||||||
|
System.out.println("直接把每行读取成字符串============================================");
|
||||||
|
|
||||||
|
//自定义每行数据的处理
|
||||||
|
ReadFileBean<AppnodeCertCsvBean> readFileBean1 = new ReadFileBean<>(2);
|
||||||
|
ReadFileBean<AppnodeCertCsvBean> bean1 = FileUtil.readBigFile(path, readFileBean1, datas -> {
|
||||||
|
if (readFileBean1.getStartRowNum() == 1) {
|
||||||
|
datas.remove(0);//跳过第一行
|
||||||
|
}
|
||||||
|
return datas.stream().map(str -> {
|
||||||
|
//自定义处理每一条数据
|
||||||
|
String[] split = str.split(",");
|
||||||
|
AppnodeCertCsvBean app = new AppnodeCertCsvBean();
|
||||||
|
app.setEnable(getValue(split, 0));
|
||||||
|
app.setDomain(getValue(split, 1));
|
||||||
|
app.setProtocol(getValue(split, 2));
|
||||||
|
app.setDeployHost(getValue(split, 3));
|
||||||
|
app.setDeployPath(getValue(split, 4));
|
||||||
|
app.setUname(getValue(split, 5));
|
||||||
|
app.setPwd(getValue(split, 6));
|
||||||
|
if (StrUtil.isNotEmpty(getValue(split, 7))) {
|
||||||
|
app.setAppnodeId(Integer.valueOf(getValue(split, 7)));
|
||||||
|
}
|
||||||
|
return app;
|
||||||
|
}).collect(Collectors.toList());
|
||||||
|
});
|
||||||
|
System.out.println("自定义每行数据的处理============================================");
|
||||||
|
System.out.println(JsonUtil.formatPrinter(bean1));
|
||||||
|
System.out.println("自定义每行数据的处理============================================");
|
||||||
|
|
||||||
|
//直接使用提供的csv文件读取
|
||||||
ReadFileBean<AppnodeCertCsvBean> readFileBean = new ReadFileBean<>(2);
|
ReadFileBean<AppnodeCertCsvBean> readFileBean = new ReadFileBean<>(2);
|
||||||
do {
|
do {
|
||||||
ReadFileBean<AppnodeCertCsvBean> bean = FileUtil.readBigFile(path, readFileBean, AppnodeCertCsvBean.class);
|
ReadFileBean<AppnodeCertCsvBean> bean = FileUtil.readBigFile(path, readFileBean, AppnodeCertCsvBean.class);
|
||||||
System.out.println("============================================");
|
System.out.println("直接使用提供的csv文件读取============================================");
|
||||||
System.out.println(JsonUtil.formatPrinter(bean));
|
System.out.println(JsonUtil.formatPrinter(bean));
|
||||||
|
System.out.println("直接使用提供的csv文件读取============================================");
|
||||||
} while (readFileBean.hasNext());
|
} while (readFileBean.hasNext());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static String getValue(String[] value, int index) {
|
||||||
|
try {
|
||||||
|
return value[index];
|
||||||
|
} catch (Exception e) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,14 +2,16 @@ package com.yexuejc.base.util.bean;
|
|||||||
|
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
|
||||||
import com.yexuejc.base.annotation.CsvToBean;
|
import com.yexuejc.base.annotation.CsvToBean;
|
||||||
|
import com.yexuejc.base.converter.IntegerNullValueDeserializer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author: yexuejc
|
* @author: yexuejc
|
||||||
* @date: 2024/2/27 10:40
|
* @date: 2024/2/27 10:40
|
||||||
*/
|
*/
|
||||||
@CsvToBean(header = "enable,domain,protocol,deployHost,deployPath,uname,pwd,appnodeId", hasHeader = true)
|
@CsvToBean(header = "enable,domain,protocol,deployHost,deployPath,uname,pwd,appnodeId")
|
||||||
public class AppnodeCertCsvBean implements Serializable {
|
public class AppnodeCertCsvBean implements Serializable {
|
||||||
/**是否生效:Y/N*/
|
/**是否生效:Y/N*/
|
||||||
private String enable;
|
private String enable;
|
||||||
@ -37,6 +39,7 @@ public class AppnodeCertCsvBean implements Serializable {
|
|||||||
/**
|
/**
|
||||||
* appnode协议时:且远程部署时,对应的远程appnode的ApiNodeId
|
* appnode协议时:且远程部署时,对应的远程appnode的ApiNodeId
|
||||||
*/
|
*/
|
||||||
|
@JsonDeserialize(using = IntegerNullValueDeserializer.class)
|
||||||
private Integer appnodeId;
|
private Integer appnodeId;
|
||||||
|
|
||||||
public String getEnable() {
|
public String getEnable() {
|
||||||
|
10
src/test/java/com/yexuejc/base/util/test.csv
Normal file
10
src/test/java/com/yexuejc/base/util/test.csv
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
是否生效,域名,部署协议,部署服务器,部署证书位置,服务器账号,服务器密码,appnodeId
|
||||||
|
N,kasm.mx.yexuejc.top,appnode,http://192.168.56.101:8888,,admin,admin,1
|
||||||
|
N,kasm.mx.yexuejc.top,appnode,local,,,,
|
||||||
|
N,shop.mx.yexuejc.top,appnode,local,,,,
|
||||||
|
Y,cloud.yexuejc.top,ssh,118.126.109.109:10371,/home/frpuser/http/cert/,frpuser,yexuejc1,
|
||||||
|
Y,blog.yexuejc.top,ssh,118.126.109.109:10371,/home/frpuser/http/cert/,frpuser,yexuejc1,
|
||||||
|
Y,yexuejc.top,ssh,118.126.109.109:10371,/home/frpuser/http/cert/,frpuser,yexuejc1,
|
||||||
|
Y,jenkins.yexuejc.top,ssh,118.126.109.109:10371,/home/frpuser/http/cert/,frpuser,yexuejc1,
|
||||||
|
Y,git.yexuejc.top,ssh,118.126.109.109:10371,/home/frpuser/http/cert/,frpuser,yexuejc1,
|
||||||
|
Y,nexus.yexuejc.top,ssh,118.126.109.109:10371,/home/frpuser/http/cert/,frpuser,yexuejc1,
|
|
Loading…
x
Reference in New Issue
Block a user