流解析上传的CSV文件工具类
事件情景
本文主要记录一下使用流解析上传的CSV文件工具类,主要有读取解析和写入输出两个方法。
1.使用依赖
import java.io.*;
2.CSVFileUtil工具类
package com.hualala.data.meta.platform.common.common.utils;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@Slf4j
public class CSVFileUtil {
/**
* 读取
* @param file csv文件(路径+文件)
* @param delimiter 分割符
* @return
*/
public static List<String[]> importCsv(File file,String delimiter,String charsetName){
List<String[]> dataList=new ArrayList<>();
BufferedReader br=null;
try {
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), charsetName);
br = new BufferedReader(isr);
String line = "";
while ((line = br.readLine()) != null) {
dataList.add(line.split(delimiter));
}
}catch (Exception e) {
}finally{
if(br!=null){
try {
br.close();
br=null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
return dataList;
}
/**
* 写入
* @param file csv文件(路径+文件名),csv文件不存在会自动创建
* @param dataList 数据
* @return
*/
public static File exportCsv(List<List<String>> exportData, String outPutPath, String fileName){
File csvFile = null;
BufferedWriter csvFileOutputStream = null;
try {
File file = new File(outPutPath);
if (!file.exists()) {
if (file.mkdirs()) {
log.info("创建成功");
} else {
log.error("创建失败");
}
}
//定义文件名格式并创建
csvFile = File.createTempFile(fileName, ".csv", new File(outPutPath));
csvFileOutputStream = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(csvFile), StandardCharsets.UTF_8), 1024);
for (List<String> exportDatum : exportData) {
writeRow(exportDatum, csvFileOutputStream);
csvFileOutputStream.newLine();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (csvFileOutputStream != null) {
csvFileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return csvFile;
}
/**
* 写一行数据
*
* @param row 数据列表
* @param csvWriter
* @throws IOException
*/
private static void writeRow(List<String> row, BufferedWriter csvWriter) throws IOException {
int i=0;
for (String data : row) {
csvWriter.write(DelQuota(data));
if (i!=row.size()-1){
csvWriter.write(",");
}
i++;
}
}
/**
* 剔除特殊字符
*
* @param str 数据
*/
public static String DelQuota(String str) {
String result = str;
String[] strQuota = {"~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "`", ";", "'", ",", ".", "/", ":", "/,", "<", ">", "?"};
for (int i = 0; i < strQuota.length; i++) {
if (result.indexOf(strQuota[i]) > -1)
result = result.replace(strQuota[i], "");
}
return result;
}
/**
* 测试
* @param args
*/
public static void main(String[] args){
exportCsv();
//importCsv();
}
/**
* CSV读取测试
* @throws Exception
*/
public static void importCsv() {
List<String[]> dataList=CSVFileUtil.importCsv(new File("F:/test_two.csv"),",","GB2312");
if(dataList!=null && !dataList.isEmpty()){
for(int i=0; i<dataList.size();i++ ){
String[] cells = dataList.get(i);
if(cells != null && cells.length > 0){
for(int index=0; index < cells.length; index ++){
System.out.print(cells[index]+" ");
}
System.out.println();
}
}
}
}
/**
* CSV写入测试
* @throws Exception
*/
public static void exportCsv() {
List<List<String>> listList=new ArrayList<>();
List<String> list1=new ArrayList<>();
List<String> list2=new ArrayList<>();
List<String> list3=new ArrayList<>();
list1.add("编号");
list1.add("姓名");
list1.add("身高");
list1.add("电话");
list2.add("1");
list2.add("小明");
list2.add("180cm");
list2.add("18941254856");
list3.add("2");
list3.add("小红");
list3.add("176cm");
list3.add("18954256325");
listList.add(list1);
listList.add(list2);
listList.add(list3);
CSVFileUtil.exportCsv(listList,"F://","testFile");
}
}