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('\t');

    public void write_csv() throws IOException {

        // 创建输出流
        Appendable out = new PrintWriter("dept2.csv");
        //CSVFormat csvFormat = CSVFormat.newFormat('\t').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" + "\t" + "产品部" + "\t" + "RSA001");
        records.add("RSA002_20180623" + "\t" + "设计部" + "\t" + "RSA002");
        records.add("RSA003_20150422" + "\t" + "技术部" + "\t" + "RSA003");
        records.add("RSA004_20160903" + "\t" + "运营部" + "\t" + "RSA004");
        records.add("RSA005_20120608" + "\t" + "财务部" + "\t" + "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('\t');
 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 + "\t" + dname + "\t" + loc);
            System.out.println(record);
        }

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

参考

  1. Apache Commons CSV User Guide
  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兀坐晴窗独饮茶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值