Java学习笔记 - Apache Common CSV 的使用总结

环境依赖

<!--common csv -->
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-csv -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-csv</artifactId>
            <version>1.7</version>
        </dependency>

将数据写入到 CSV文件

// 要读读取的csv文件的 headr
    private final String[] header = new String[]{
          
   "deptno", "dname" ,"loc"};
    private final String FILE_NAME = "H:\source\gitRep\BigdataStudy\Kylin\Kylin\data\dept.csv";

    // 注意 : withSkipHeaderRecord 这里要设置跳过 header, 否则会吧 header 当成第一行记录
    // 如果默认分隔符不是 , 则需要手动设置

    CSVFormat csvFormat = CSVFormat.DEFAULT
            .withHeader(header)
            .withSkipHeaderRecord()
            // 设置分隔符
            .withDelimiter(	);

    public void write_csv() throws IOException {
          
   

        // 创建输出流
        Appendable out = new PrintWriter("dept2.csv");
        //CSVFormat csvFormat = CSVFormat.newFormat(	).withHeader(header).withSkipHeaderRecord();
        // 注意 使用 DEFAULT 创建的CSVFormat对象生成记录可以自动换行,
        // 如果使用上面的 newFormat 的csvformat 不会自动换行
        CSVPrinter csvPrinter = CSVFormat.DEFAULT.print(out);
       /* CSVPrinter csvPrinter = new CSVPrinter(out, csvFormat);*/

        // 创建 一些记录信息
        List<String> records = new ArrayList<>();
        // 部门编号,
        records.add("RSA001_20190121" + "	" + "产品部" + "	" + "RSA001");
        records.add("RSA002_20180623" + "	" + "设计部" + "	" + "RSA002");
        records.add("RSA003_20150422" + "	" + "技术部" + "	" + "RSA003");
        records.add("RSA004_20160903" + "	" + "运营部" + "	" + "RSA004");
        records.add("RSA005_20120608" + "	" + "财务部" + "	" + "RSA005");

        // 将数据写入到 文件中
        for (String record : records) {
          
   
            csvPrinter.printRecord(record);
        }

        // 刷写缓冲区
        csvPrinter.flush();
        csvPrinter.close();
    }

从CSV文件中读取数据

// 要读读取的csv文件的 headr
    private final String[] header = new String[]{
          
   "deptno", "dname", "loc"};
    private final String FILE_NAME = "H:\source\gitRep\BigdataStudy\Kylin\Kylin\data\dept.csv";

    // 注意 : withSkipHeaderRecord 这里要设置跳过 header, 否则会吧 header 当成第一行记录
    // 如果默认分隔符不是 , 则需要手动设置

    CSVFormat csvFormat = CSVFormat.DEFAULT
            .withHeader(header)
            .withSkipHeaderRecord()
            // 设置分隔符
            .withDelimiter(	);
 public void read_csv() throws IOException {
          
   

        // 将文件转换为 Reader 流
        Reader in = new FileReader(FILE_NAME);
        // 解析数据
        CSVParser parser = csvFormat.parse(in);
        // 获取记录
        List<CSVRecord> records = parser.getRecords();
        // 打印记录
        for (CSVRecord record : records) {
          
   
            // 获取指定字段的数据
            String deptno = record.get("deptno");
            String dname = record.get("dname");
            String loc = record.get("loc");

            System.out.println(deptno + "	" + dname + "	" + loc);
            System.out.println(record);
        }

        // 关闭流
        parser.close();
        in.close();
    }

参考

经验分享 程序员 微信小程序 职场和发展