Java 操作CSV 文件

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,948评论 18 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,958评论 6 342
  • 个人笔记,方便自己查阅使用 Contents Java LangAssignment, ReferenceData...
    freenik阅读 1,411评论 0 6
  • 文章作者:Tyan博客:noahsnail.com 2.Introduction to the Spring Fr...
    SnailTyan阅读 5,419评论 7 56
  • “你小时候我们家里还很穷!”这话是老妈说的,但我并不觉得,或者说对于小娃娃来说没有穷不穷的讲究,有吃有玩就是富足的...
    桑紫阅读 631评论 4 3