Apache Commons工具类学习(一)-----CSV

从今天开始学习Apache Commons工具类中的部分组建,第一项内容为:CSV组件

对应官网地址:http://commons.apache.org/proper/commons-csv/index.html

下载地址:http://commons.apache.org/proper/commons-csv/download_csv.cgi

JavaDoc:http://commons.apache.org/proper/commons-csv/apidocs/index.html

用户指引:http://commons.apache.org/proper/commons-csv/user-guide.html

Maven引用: 

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-csv</artifactId>
    <version>1.2</version>
</dependency>

一、csv写入

  步骤:

  1、初始化csv文件

    

//CSV文件分隔符
private final static String NEW_LINE_SEPARATOR="
";
//初始化csvformat
CSVFormat formator = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR);

  2、创建对应文件的writer对象

    

//创建FileWriter对象,filePathcsv文件路径
FileWriter fileWriter=new FileWriter(filePath);

  3、使用CSVPrinter中的printRecord 方法写入文件

    printRecord方法存在多种传参

    printRecord(Iterable<?> values) Iterable接口对象,所有集成Iterable接口的子类均可写入

    printRecord(Object... values)对象数组,可以将数据整合为各种类型的对象,写入的为Object的toString()方法的值

    批量写入时使用 printRecords, 同样有printRecords(Iterable<?> values), printRecords(Object... values) 两个方法

    

  4、样例代码

 1 /**写入csv文件
 2      * @param headers 列头
 3      * @param data 数据内容
 4      * @param filePath 创建的csv文件路径
 5      * @throws IOException **/
 6     public static void writeCsv(String[] headers,List<String[]> data,String filePath) throws IOException{
 7         
 8         //初始化csvformat
 9         CSVFormat formator = CSVFormat.DEFAULT.withRecordSeparator(NEW_LINE_SEPARATOR);
10         
11         //创建FileWriter对象
12         FileWriter fileWriter=new FileWriter(filePath);
13         
14         //创建CSVPrinter对象
15         CSVPrinter printer=new CSVPrinter(fileWriter,formator);
16         
17         //写入列头数据
18         printer.printRecord(headers);
19         
20         if(null!=data){
21             //循环写入数据
22             for(String[] lineData:data){
23                 
24                 printer.printRecord(lineData);
25                 
26             }
27         }
28         
29         System.out.println("CSV文件创建成功,文件路径:"+filePath);
30         
31     }

   5、调用代码及结果

    

    真实写入结果

    

    

二、读取csv

  读取csv文件需要使用CSVParse类,使用此类来读取csv字节,与写入时类似,都首先要用CSVFormat对象来格式化csv文件流,此时要使用Reader的子类对象

  一下为样例:

  

    /**读取csv文件
     * @param filePath 文件路径
     * @param headers csv列头
     * @return CSVRecord 列表
     * @throws IOException **/
    public static List<CSVRecord> readCSV(String filePath,String[] headers) throws IOException{
        
        //创建CSVFormat
        CSVFormat formator = CSVFormat.DEFAULT.withHeader(headers);
        
        FileReader fileReader=new FileReader(filePath);
        
        //创建CSVParser对象
        CSVParser parser=new CSVParser(fileReader,formator);
        
        List<CSVRecord> records=parser.getRecords();
        
        parser.close();
        fileReader.close();
        
        return records;    
    }

  调用代码及结果(循环时索引从1开始,是因为返回的records中包含了列头)

  

三、CSVRecord

  一下为此类源码部分,可以看到数据存储在String数组values 中,并有mapping来对应索引关系,所以在使用get()取数据时有get(String name)使用列名取 和 get(int index)索引值来取数据,

  如果想深入学习可以在下载时同时下载源码,深入数据结构来详细了解更详细的用法

  

  

原文地址:https://www.cnblogs.com/LeeForLeslie/p/5155775.html