Java中csv文件的写与读

Java中csv文件的写与读

依赖配置

  • pom.xml文件中,配置javacsv工具包

     

    <dependency>
        <groupId>net.sourceforge.javacsv</groupId>
        <artifactId>javacsv</artifactId>
        <version>2.0</version>
    </dependency>
    

写csv文件

  • 简单示例写函数

     

    public static void writeCsv(String filePath) {
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                file.createNewFile();
            }
            // 创建CSV写对象
            CsvWriter csvWriter = new CsvWriter(filePath, ',', Charset.forName("GBK"));
            // 写表头
            String[] headers = {"content"};
            csvWriter.writeRecord(headers);
            //写内容
            String[] content1 = {"hello"};
            String[] content2 = {"world"};
            csvWriter.writeRecord(content1);
            csvWriter.writeRecord(content2);
            csvWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

读取csv文件

  • 简单实例读函数

     

     public static void readScv(String filePath) {
        File file = new File(filePath);
        if (!file.exists()) {
            System.out.println("文件不存在!");
            return;
        }
        try {
            // 创建CSV读对象
            CsvReader csvReader = new CsvReader(filePath, ',', Charset.forName("GBK"));
            // 读表头
            csvReader.readHeaders();
            while (csvReader.readRecord()) {
                // 读取一行数据
                System.out.println(csvReader.getRawRecord());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

在实践中暂时遇到的问题

  1. 当数字过长会变成科学计数法
    • 如:140108199212301237变成140108199212300000【解决方案:制表符和true的配合使用(先留个思考,在下面的函数中会有体现)】
  2. 编码问题
    • 使用UTF-8读文件时,存在乱码问题【解决方案:读写都采用使用GBK编码】

函数升级改造

Csv文件写函数

  • 封装的写函数

     

    public static void read(String filePath,String column_name) {
        File file = new File(filePath);
        if (!file.exists()) {
            System.out.println("文件不存在!");
            return;
        }
        try {
            // 创建CSV读对象
            CsvReader csvReader = new CsvReader(filePath, ',', Charset.forName("GBK"));
            // 读表头
            csvReader.readHeaders();
            while (csvReader.readRecord()) {
                // 读这行的某一列
                System.out.println(csvReader.get(column_name));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

Csv文件度函数

  • 封装的度函数

     

    public static void write(String filePath, String[] headers, List<String[]> datas) {
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                file.createNewFile();
            }
            // 创建CSV写对象
            CsvWriter csvWriter = new CsvWriter(filePath, ',', Charset.forName("GBK"));
            // 写表头
            csvWriter.writeRecord(headers);
            if (datas != null && datas.size() > 0) {
                for (String[] temp : datas) {
                    for (int i = 0; i < temp.length; i++) {
                        //防止数字过长出现科学计数法的问题【\t和true来解决】
                        temp[i] = temp[i] + "\t";
                    }
                    csvWriter.writeRecord(temp, true);
                }
            }
            csvWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    

其他的遇到问题自己拓展

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

乘风御浪云帆之上

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

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

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

打赏作者

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

抵扣说明:

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

余额充值