CSV是逗号分隔文件(Comma Separated Values)的首字母英文缩写,是一种用来存储数据的纯文本格式,通常用于电子表格或数据库软件。在 CSV文件中,数据“栏”以逗号分隔,可允许程序通过读取文件为数据重新创建正确的栏结构,并在每次遇到逗号时开始新的一栏。
一、利用javacsv2.0操作csv文件:
package com.iflytek.demo; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; import com.csvreader.CsvReader; import com.csvreader.CsvWriter; /** * 利用javacsv2.0做导入导出csv文件工具类<br/> * * * @author kpchen * */ public class CSVUtil { static char separator = ','; public static void main(String[] args) throws Exception { // 测试导出 String filePath = "D:/test.csv"; List<String[]> dataList = new ArrayList<String[]>(); for (int i = 0; i < 10; i++) { dataList.add(new String[] { "0" + i, "小明" + i, "java" + i }); } exportCsv(dataList, filePath); // 测试导入 List<String[]> datas = importCsv(filePath); for (String[] strings : datas) { System.out.println(strings[0]); } } /** * java导入csv文件 * * @param filePath * 导入路径 * @return * @throws Exception */ public static List<String[]> importCsv(String filePath) throws Exception { CsvReader reader = null; List<String[]> dataList = new ArrayList<String[]>(); try { reader = new CsvReader(filePath, separator, Charset.forName("GBK")); // 读取表头 加上这一句是不算表头数据从第二行开始取 reader.readHeaders(); // 逐条读取记录,直至读完 while (reader.readRecord()) { dataList.add(reader.getRawRecord().split(",")); // // 下面是几个常用的方法 // 读取一条记录 System.out.println(reader.getRawRecord()); // 按列名读取这条记录的值 System.out.println(reader.get(0)); System.out.println(reader.get(1)); System.out.println(reader.get(2)); System.out.println(reader.get(3)); } } catch (Exception e) { System.out.println("读取CSV出错..." + e); throw e; } finally { if (null != reader) { reader.close(); } } return dataList; } /** * java导出cvs文件 * * @param dataList * 数据集 * @param filePath * 导出路径 * @return * @throws Exception */ public static boolean exportCsv(List<String[]> dataList, String filePath) throws Exception { boolean isSuccess = false; CsvWriter writer = null; FileOutputStream out = null; try { out = new FileOutputStream(filePath, true); writer = new CsvWriter(out, separator, Charset.forName("GBK")); for (String[] strs : dataList) { writer.writeRecord(strs); } isSuccess = true; } catch (Exception e) { System.out.println("生成CSV出错..." + e); throw e; } finally { if (null != writer) { writer.close(); } if (null != out) { try { out.close(); } catch (IOException e) { System.out.println("exportCsv close Exception: " + e); throw e; } } } return isSuccess; } }
二、利用流操作csv文件
package com.iflytek.demo; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.List; public class CSVUtil2 { public static void main(String[] args) { // 测试 导出 // List<String> dataList = new ArrayList<String>(); // dataList.add("1,张三,男"); // dataList.add("2,李四,男"); // dataList.add("3,小红,女"); // boolean isSuccess = exportCsv(new File("D:/test.csv"), dataList); // System.out.println(isSuccess); // 测试 导入 List<String> dataList = importCsv(new File("D:/test.csv")); if (dataList != null && !dataList.isEmpty()) { for (String data : dataList) { System.out.println(data); } } } /** * 导出 * * @param file * csv文件(路径+文件名),csv文件不存在会自动创建 * @param dataList * 数据 * @return */ public static boolean exportCsv(File file, List<String> dataList) { boolean isSucess = false; FileOutputStream out = null; OutputStreamWriter osw = null; BufferedWriter bw = null; try { out = new FileOutputStream(file); osw = new OutputStreamWriter(out); bw = new BufferedWriter(osw); if (dataList != null && !dataList.isEmpty()) { for (String data : dataList) { bw.append(data).append(" "); } } isSucess = true; } catch (Exception e) { isSucess = false; } finally { if (bw != null) { try { bw.close(); bw = null; } catch (IOException e) { e.printStackTrace(); } } if (osw != null) { try { osw.close(); osw = null; } catch (IOException e) { e.printStackTrace(); } } if (out != null) { try { out.close(); out = null; } catch (IOException e) { e.printStackTrace(); } } } return isSucess; } /** * 导入 * * @param file * csv文件(路径+文件) * @return */ public static List<String> importCsv(File file) { List<String> dataList = new ArrayList<String>(); BufferedReader br = null; try { br = new BufferedReader(new FileReader(file)); String line = ""; while ((line = br.readLine()) != null) { dataList.add(line); } } catch (Exception e) { } finally { if (br != null) { try { br.close(); br = null; } catch (IOException e) { e.printStackTrace(); } } } return dataList; } }