package com.file;
import com.ZgxLoggerUtil;
import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;
import org.apache.log4j.Logger;
import javax.swing.*;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
/**
* @author Jarvis
* @version 1.0
* @time 2019/12/20 9:35
*/
public class ZgxFileUtil {
private static Logger log = ZgxLoggerUtil.getLogger(ZgxFileUtil.class);
/**
* 功能:将数据写入csv文件中
*
* @param filePath 文件路径
* @param header 文件头
* @param contents 文件内容
* @return
*/
static public boolean writeCsv(String filePath, String[] header, List<String[]> contents) {
CsvWriter csvWriter = new CsvWriter(filePath, ',', Charset.forName("GBK"));
// 写入文件头
if (header != null) {
try {
csvWriter.writeRecord(header);
} catch (IOException e) {
e.printStackTrace();
}
}
// 写入文件行
for (int i = 0; i < contents.size(); i++) {
try {
csvWriter.writeRecord(contents.get(i));
} catch (IOException e) {
e.printStackTrace();
}
}
csvWriter.close();
return true;
}
static public boolean writeCsv(String filePath, String[] header, List<String[]> contents, JProgressBar jProgressBar, List<String> fileType) {
List<CsvWriter> csvWriterList = new ArrayList<>();
for (int i = 0; i < fileType.size(); i++) {
csvWriterList.add(new CsvWriter(String.format("%s.%s", filePath, fileType.get(i)), ',', Charset.forName("GBK")));
}
// 写入文件头
if (header != null) {
try {
for (int i = 0; i < csvWriterList.size(); i++) {
csvWriterList.get(i).writeRecord(header);
}
} catch (IOException e) {
e.printStackTrace();
}
}
// 写入文件行
try {
for (int i = 0; i < contents.size(); i++) {
for (int j = 0; j < csvWriterList.size(); j++) {
csvWriterList.get(j).writeRecord(contents.get(i));
System.out.println(String.format("写入“%s”文件 第%s行", csvWriterList.get(j), i + 1));
}
jProgressBar.setValue(i + 1);
System.out.println(String.format("总:%s 最小:%s 最大:%s 当前:%s", contents.size(), jProgressBar.getMinimum(), jProgressBar.getMaximum(), i));
}
for (int i = 0; i < csvWriterList.size(); i++) {
csvWriterList.get(i).close();
}
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
/**
* 功能:读取CSV文件
*
* @param readHeaders 是否要读文件头
* @param filePath 文件路径
* @return 返回读取的数据
*/
static public List<String[]> readCsv(boolean readHeaders, String filePath) {
List<String[]> contentHangs = new ArrayList<>();
try {
CsvReader csvReader = new CsvReader(filePath, ',', Charset.forName("GBK"));
if (!readHeaders) {
csvReader.readHeaders();
}
while (csvReader.readRecord()) {
contentHangs.add(csvReader.getValues());
}
csvReader.close();
} catch (IOException e) {
e.printStackTrace();
}
int hang = 0;
int lie = 0;
log.info(String.format("第%s行第%s列的数据=%s", hang, lie, contentHangs.get(hang)[lie]));
return contentHangs;
}
/**
* 功能:数据清洗(读取一个旧文件的数据 清洗预期的数据后 再写入到一个新的文件中)
* 备注:清洗逻辑需变更
*
* @param oldFilePath 原文件路径
* @param newFilePath 清洗后的新文件路径
*/
static public void readAndWriteCsv(String oldFilePath, String newFilePath) {
/** 【清洗原理】从idb上导出的数据 有双引号引起来的 所以要清洗下数据 取双引号中间的数据*/
// step1 读取文件 清洗数据
List<String[]> read = readCsv(false, oldFilePath);
List<String[]> newData = new ArrayList<>();
for (int i = 0; i < read.size(); i++) {
String[] substring = {read.get(i)[0].substring(0, read.get(i)[0].length())};
newData.add(substring);
}
// step2 将清洗后的数据写入文件
writeCsv(newFilePath, null, newData);
}
}