`
小网客
  • 浏览: 1216450 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

apache common之CSV文件操作

    博客分类:
  • Java
 
阅读更多

依赖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
0
分享到:
评论
1 楼 json20080301 2015-03-19  
MARK!这种开源类库都有,以前都是自己写

相关推荐

Global site tag (gtag.js) - Google Analytics