-
Notifications
You must be signed in to change notification settings - Fork 0
read and write csv file
yangyp8110 edited this page Aug 22, 2018
·
3 revisions
<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>
public class CsvUtils {
private static final Charset DEFAULT_CHARSET = Charset.forName("GB2312");
/**
* 读取Json String
* @param filePath
* @param fields
* @return
* @throws IOException
*/
public static String read(String filePath, String[] fields) throws IOException {
return read(filePath, fields, DEFAULT_CHARSET);
}
/**
* 读取Json String
* @param filePath
* @param fields Json key(按csv表头格式传入对应的列字段名)
* @param charset
* @return
* @throws IOException
*/
public static String read(String filePath, String[] fields, Charset charset) throws IOException {
// 创建CSV读对象
@Cleanup
CsvReader csvReader = new CsvReader(filePath, ',', charset);
// 读表头
csvReader.readHeaders();
// 获取表头
String[] headers = csvReader.getHeaders();
if(fields == null || fields.length <= 0) {
fields = csvReader.getHeaders();
}
StringBuilder sb = new StringBuilder();
sb.append("[");
while (csvReader.readRecord()) {
sb.append("{");
for (int i = 0; i < fields.length; i++) {
sb.append("\"").append(fields[i]).append("\":\"").append(csvReader.get(headers[i])).append("\"");
if (i < fields.length - 1) {
sb.append(",");
}
}
sb.append("},");
}
if (sb.lastIndexOf(",") > 0) {
sb.deleteCharAt(sb.lastIndexOf(",")).append("]");
}
return sb.toString();
}
/**
* 写入csv
* @param filePath
* @param headers
* @param content
* @throws IOException
*/
public static void write(String filePath, String[] headers, List<String[]> content) throws IOException {
write(filePath, headers, content, DEFAULT_CHARSET);
}
/**
* 写入csv文件
* @param filePath 写入路径
* @param headers 表头
* @param content 内容
* @param charset 编码
* @throws IOException
*/
public static void write(String filePath, String[] headers, List<String[]> content, Charset charset) throws IOException {
// 创建CSV写对象
@Cleanup
CsvWriter csvWriter = new CsvWriter(filePath, ',', charset);
// 写表头
csvWriter.writeRecord(headers);
// 写行数据
int i = 0;
for (String[] rowTxt : content) {
i++;
csvWriter.writeRecord(rowTxt);
if (i == 10000) {
csvWriter.flush();
i = 0;
}
}
csvWriter.flush();
}
}
private static final String[] header = {"列1", "列2", "列3", "列4", "列5", "列6", "列7", "列8", "列9", "列10", "列11"};
private static final String[] headerCol = {"id", "col1", "col2", "col3", "col4", "col5", "col6", "col7", "col8", "col9", "col10"};
public static void writeCsvData(String filePath, Integer count) throws IOException {
long l = System.currentTimeMillis();
// 表头
String[] headers = header;
List<DataDemo> dataList = getDataList(count);
List<String[]> dataRows = new ArrayList<>(dataList.size());
// 添加表头
for (DataDemo data : dataList) {
String[] rowTxt = new String[]{String.valueOf(data.getId()), data.getCol1(), data.getCol2(), data.getCol3(),
data.getCol4(), data.getCol5(), data.getCol6(), data.getCol7(), data.getCol8(), data.getCol9(), data.getCol10()};
dataRows.add(rowTxt);
}
CsvUtils.write(filePath, headers, dataRows, Charset.forName("GB2312"));
System.out.println("write csv data:" + dataRows.size() + ",cost:" + (System.currentTimeMillis() - l));
}
public static void readCsvData(String filePath) throws IOException {
long l = System.currentTimeMillis();
String[] fields = headerCol;
String read = CsvUtils.read(filePath, fields);
List<DataDemo> infoList = JSON.parseArray(read, DataDemo.class);
System.out.println("read csv data:" + infoList.size() + ",cost:" + (System.currentTimeMillis() - l));
}