CSVOperation.java 689 B

1234567891011121314151617181920212223242526272829
  1. package me.yoqi.test;
  2. import java.io.FileReader;
  3. import java.io.IOException;
  4. import java.io.Reader;
  5. import org.apache.commons.csv.CSVFormat;
  6. import org.apache.commons.csv.CSVRecord;
  7. /**
  8. * 官方文档:http://commons.apache.org/proper/commons-csv/user-guide.html
  9. * 还有一个javacsv包更人性化。
  10. * @author liuyuqi
  11. *
  12. */
  13. public class CSVOperation {
  14. public void write() {
  15. }
  16. public void read() throws IOException {
  17. Reader in = new FileReader("path/to/file.csv");
  18. Iterable<CSVRecord> records = CSVFormat.EXCEL.parse(in);
  19. for (CSVRecord record : records) {
  20. String lastName = record.get("Last Name");
  21. String firstName = record.get("First Name");
  22. }
  23. }
  24. }