Java操作CSV文件工具类

本文介绍了如何使用Apache Commons CSV库在Java中方便地读写CSV文件,包括数据读取、文件路径操作、数据保存以及字符串格式转换。通过实例演示了CSVUtil类中的关键方法及其在实际项目中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java操作CSV文件工具类

  1. 引入Maven依赖

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-csv</artifactId>
    <version>1.3</version>
</dependency>
  1. CSVUtil

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVPrinter;
import org.apache.commons.csv.CSVRecord;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Tuyq
 * @date 2022-10-22
 * @desc 处理CSV文件工具类
 */
public class CSVUtil {

    private CSVUtil() {
    }


    /**
     * 读取数据
     *
     * @param stream  输入流
     * @param charset 字符集
     * @return
     */
    public static List<List<String>> getData(InputStream stream, String charset) {
        try {
            List<List<String>> dataList = new ArrayList<>();
            Reader reader = new InputStreamReader(stream, charset);
            CSVParser parse = CSVFormat.DEFAULT.parse(reader);
            List<CSVRecord> records = parse.getRecords();
            for (int i = 0; i < records.size(); i++) {
                List<String> dataItem = new ArrayList<>();
                CSVRecord record = records.get(i);
                for (int j = 0; j < record.size(); j++) {
                    dataItem.add(record.get(j));
                }
                dataList.add(dataItem);
            }
            return dataList;
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }

    }

    /**
     * 读取数据
     *
     * @param filePath 文件路径
     * @param charset  字符集
     * @return
     */
    public static List<List<String>> getData(String filePath, String charset) {
        FileInputStream fin = null;
        try {
            fin = new FileInputStream(filePath);
            return getData(fin, charset);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } finally {
            if (fin != null) {
                try {
                    fin.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    /**
     * @param os       输出流
     * @param charset  字符格式
     * @param dataList 数据列表
     */
    public static void saveData(OutputStream os, String charset, List<List<String>> dataList) {
        try {
            OutputStreamWriter osw = new OutputStreamWriter(os, charset);
            CSVPrinter printer = new CSVPrinter(osw, CSVFormat.DEFAULT);
            for (List values : dataList) {
                printer.printRecord(values);
                osw.flush();
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * @param dataList 数据列表
     * @return 数据字符格式
     */
    public static String getStrData(List<List<String>> dataList) {
        try {
            StringWriter sw = new StringWriter();
            CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT);
            for (List<String> values : dataList) {
                printer.printRecord(values);
            }
            return sw.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值