JAVA读写CSV文件

最近工作需要,需要读写CSV文件的数据,简单封装了一下

依赖

读写CSV文件只需引用javacsv这个依赖就可以了


    net.sourceforge.javacsv
    javacsv
    2.0

读文件

/**
     * Read from CSV
     *
     * @param separator 分隔符
     * @param filePath 文件路径
     * @return
     * */
    public static List readFromCSV(Character separator, String filePath) {
        CsvReader reader = null;
        List result = new ArrayList<>();
        try {
            //如果生产文件乱码,windows下用gbk,linux用UTF-8
            reader = new CsvReader(filePath, separator, Charset.forName("GBK"));

            // 读取标题
            reader.readHeaders();
            // 逐条读取记录,直至读完
            while (reader.readRecord()) {
                //读取第一例
                reader.get(0);
                //读取指定名字的列
                reader.get("");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != reader) {
                reader.close();
            }
        }

        return result;
    }

因为甜大王比较喜欢将读取的一列封装为一个Object,同时为了工具更通用返回的结果类型是一个List

写文件

/**
     * Write into CSV
     *
     * @param separator 分隔符
     * @param filePath 文件路径
     * @param strList 对应CSV中的一行记录
     * */
    public static void writeIntoCSV(Character separator, String filePath, List> strList) {
        CsvWriter csvWriter = null;
        try {
            // 创建CSV写对象
            csvWriter = new CsvWriter(filePath, separator, Charset.forName("GBK"));
            // 写标题
            //String[] headers = {"FileName","FileSize","FileMD5"};
            //csvWriter.writeRecord(headers);
            for (List list : strList) {
                    String[] writeLine = new String[list.size()];
                    list.toArray(writeLine);
                    csvWriter.writeRecord(writeLine);
            }

            csvWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != csvWriter) {
                csvWriter.close();
            }
        }
    }

完整代码

public class CSVTool {
    /**
     * Read from CSV
     *
     * @param separator 分隔符
     * @param filePath 文件路径
     * @return
     * */
    public static List readFromCSV(Character separator, String filePath) {
        CsvReader reader = null;
        List result = new ArrayList<>();
        try {
            //如果生产文件乱码,windows下用gbk,linux用UTF-8
            reader = new CsvReader(filePath, separator, Charset.forName("GBK"));

            // 读取标题
            reader.readHeaders();
            // 逐条读取记录,直至读完
            while (reader.readRecord()) {
                //读取第一例
                reader.get(0);
                //读取指定名字的列
                reader.get("");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (null != reader) {
                reader.close();
            }
        }

        return result;
    }

    /**
     * Write into CSV
     *
     * @param separator 分隔符
     * @param filePath 文件路径
     * @param strList 对应CSV中的一行记录
     * */
    public static void writeIntoCSV(Character separator, String filePath, List> strList) {
        CsvWriter csvWriter = null;
        try {
            // 创建CSV写对象
            csvWriter = new CsvWriter(filePath, separator, Charset.forName("GBK"));
            // 写标题
            //String[] headers = {"FileName","FileSize","FileMD5"};
            //csvWriter.writeRecord(headers);
            for (List list : strList) {
                    String[] writeLine = new String[list.size()];
                    list.toArray(writeLine);
                    csvWriter.writeRecord(writeLine);
            }

            csvWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != csvWriter) {
                csvWriter.close();
            }
        }
    }
}



附:[完整代码]()

你可能感兴趣的:(java)