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("*****************");
}

?

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。