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="\n";
//初始化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)索引值來取數據,

  如果想深入學習可以在下載時同時下載源碼,深入數據結構來詳細了解更詳細的用法

  

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM