CSV是逗號分隔文件(Comma Separated Values)的首字母英文縮寫,是一種用來存儲數據的純文本格式,一般用於電子表格或數據庫軟件。在 CSV文件中,數據「欄」以逗號分隔,可容許程序經過讀取文件爲數據從新建立正確的欄結構,並在每次遇到逗號時開始新的一欄。java
1、利用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; } }
2、利用流操做csv文件app
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("\r\n"); } } 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; } }
源碼:http://pan.baidu.com/s/1nt1r7ap工具