Maven pom dependency
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.3</version>
</dependency>
讀取CSV 文件
public void printCsvFile(String fileName) {
Reader fileReader = new FileReader(fileName);
Iterable<CSVRecord> records = CSVFormat.RFC4180.withFirstRecordAsHeader().parse(fileReader);
for (CSVRecord record : records) {
System.out.println(record.get("instanceId") + record.get("regionId") + record.get("zoneId"))
}
}
寫入CSV文件
public void writeCsvFile(String fileName) {
Appendable fileWriter = new FileWriter(fileName);
CSVPrinter printer = CSVFormat.RFC4180.withHeader("instanceId", "regionId", "zoneId").print(fileWriter)
printer.printRecord("testInstanceId", "testRegionId", "testZoneId");
printer.close();
}
Reference
http://commons.apache.org/proper/commons-csv/user-guide.html