package cn.gov.cnis.db; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.supercsv.io.CsvListReader; import org.supercsv.io.CsvListWriter; import org.supercsv.prefs.CsvPreference; public class OperateCsv { /** * 读取csv文件(不带头部) * * @param String filePath * @return csv文件组装成list * @throws IOException */ static public List<String[]> getContentFromFile(String filePath) throws IOException { File file = new File(filePath); List<String[]> content = new ArrayList<String[]>(); CsvListReader reader = new CsvListReader(new FileReader (file), CsvPreference.EXCEL_PREFERENCE); reader.getCSVHeader(true);// 去除头部字段声明 List<String> line = new ArrayList<String>(); while ((line = reader.read()) != null) { content.add(line.toArray(new String[] {})); } return content; } /** * 读取csv文件(带头部) * * @param String filePath * @return csv文件组装成list * @throws IOException */ static public List<String[]> getDetailFromFile(String filePath) throws IOException { File file = new File(filePath); List<String[]> content = new ArrayList<String[]>(); CsvListReader reader = new CsvListReader(new FileReader(file), CsvPreference.EXCEL_PREFERENCE); String[] header = reader.getCSVHeader(true); content.add(header); List<String> line = new ArrayList<String>(); while ((line = reader.read()) != null) { content.add(line.toArray(new String[] {})); } return content; } /** * 读取csv文件的头部 * * @param String filePath * @return csv文件的头部 * @throws IOException */ static public String[] getHeaderFromFile(String filePath) throws IOException { File file = new File(filePath); CsvListReader reader = new CsvListReader(new FileReader (file), CsvPreference.EXCEL_PREFERENCE); return reader.getCSVHeader(true); } /** * 写入csv文件 * * @param String filePath * @param header * 头部 * @param content * 内容 * @throws IOException */ static public void writeToCsv(String filePath, String[] header, List<String[]> content) throws IOException { File file = new File(filePath); CsvListWriter writer = new CsvListWriter(new FileWriter(file), CsvPreference.EXCEL_PREFERENCE); writer.writeHeader(header); for (String[] str : content) { writer.write(str); } writer.close(); } /** * 写入csv文件 * * @param String filePath * @param content * 内容 * @throws IOException */ static public void writeContentToCsv(String filePath, List<String[]> content) throws IOException { File file = new File(filePath); CsvListWriter writer = new CsvListWriter(new FileWriter(file),CsvPreference.EXCEL_PREFERENCE); for (String[] str : content) { writer.write(str); } writer.close(); } /** * 写入csv文件(头部) * * @param String filePath * @param content * 内容 * @throws IOException */ static public void writeHeaderToCsv(String filePath, String[] header) throws IOException { File file = new File(filePath); CsvListWriter writer = new CsvListWriter(new FileWriter(file),CsvPreference.EXCEL_PREFERENCE); writer.writeHeader(header); writer.close(); } public static void main(String[] args) throws IOException { OperateCsv operateCsv = new OperateCsv(); String file = "e:/Projects/Java/luke-3.4.0_1/AVDDOCS/export/73602.csv"; List<String[]> content = operateCsv.getDetailFromFile(file); String[] header = operateCsv.getHeaderFromFile(file); for (String[] str : content) { for (int i = 0; i < str.length; i++) { System.out.println(str[i] + " " + str[i + 1] + " " + str[i + 2] + " " + str[i + 3]); i = i + 4; // System.out.println(str[i]); } String file1 = "D:/2.csv"; operateCsv.writeToCsv(file1, header, content); operateCsv.writeHeaderToCsv(file1, header); operateCsv.writeContentToCsv(file1, content); } } }