xiayu 发表于 2017-1-6 09:53:01

apache common之CSV文件操作

  依赖jar:

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.0</version>
</dependency>
  写操作:

List<String[]> data = new ArrayList<String[]>();
data.add(new String[] { "A", "B", "C" });
data.add(new String[] { "1", "2", "3" });
data.add(new String[] { "A1", "B2", "C3" });
FileWriter fw = new FileWriter(new File("c:/linkrmb.com.csv"));
final CSVPrinter printer = CSVFormat.EXCEL.print(fw);
printer.printRecords(data);
printer.flush();
printer.close();
  读操作:

String path = "c:/linkrmb.com.csv";
InputStream inputStream = new FileInputStream(path);
InputStreamReader isr = new InputStreamReader(inputStream);
Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(isr);
for (CSVRecord record : records) {
for (String string : record) {
System.out.print(string);
System.out.print("-");
}
System.out.println();
System.out.println("*****************");
}
页: [1]
查看完整版本: apache common之CSV文件操作