前言
对于单次大数据量的存储,从mysql中读取或者写入由于耗时及性能问题, 可以选择clickhouse等大数据存储或者 可以以文件形式存入服务器中。
而txt,csv文件相对占用内容较小,所以选用这两种方式。
txt文件:
写文件:
public static void writeToTxt(List list, String path, String fileName) {
File parent = new File(path);
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
FileOutputStream outSTr = null;
BufferedOutputStream Buff = null;
String enter = "\r\n";
StringBuffer write;
path = path.concat(File.separator).concat(fileName);
try {
outSTr = new FileOutputStream(path);
Buff = new BufferedOutputStream(outSTr);
for (int i = 0; i < list.size(); i++) {
write = new StringBuffer();
write.append(list.get(i));
write.append(enter);
Buff.write(write.toString().getBytes("UTF-8"));
}
Buff.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
Buff.close();
outSTr.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
读文件:
public static List<String> readFileTxt(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
List<String> result = new ArrayList<>();
try {
reader = new BufferedReader(new FileReader(file));
String tempStr;
while ((tempStr = reader.readLine()) != null) {
result.add(tempStr);
}
return result;
} catch (IOException e) {
log.error("TxtUtils 读txt文件报错:", e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
log.error("TxtUtils 读txt文件关闭流报错:", e1);
}
}
}
return result;
}
csv文件:
对于csv文件,我选用了一个开源的工具类Hutool,大家可以去官网查看官方文档。
public static void write() {
String fileName = "testDemo1" + ".csv";
String path="/usr/data/";
CsvWriter writer = CsvUtil.getWriter(path + fileName, CharsetUtil.CHARSET_UTF_8);
List<String> list = new ArrayList<>();
list.add("id,name,userId");
list.add("1,zhangsan1,10");
list.add("2,zhangsan2,20");
writer.write(list);
}
public static void read() {
String fileName = "testDemo1.csv";
CsvReader reader = CsvUtil.getReader();
List<CSVDemoData> result = reader.read(ResourceUtil.getUtf8Reader(path + fileName), CSVDemoData.class);
log.info("读取结果为:{}", JSON.toJSONString(result));
}