欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一文詳解Java如何使用commons-csv搞定CSV文件操作

 更新時間:2025年02月11日 10:27:49   作者:五行星辰  
在?Java?開發(fā)中,處理?CSV(逗號分隔值)文件是一項常見的任務(wù),本文將利用Apache?Commons?CSV?庫實現(xiàn)讀取,寫入和操作CSV文件,希望對大家有所幫助

在 Java 開發(fā)中,處理 CSV(逗號分隔值)文件是一項常見的任務(wù)。Apache Commons CSV 庫就是一個非常強(qiáng)大且易用的工具,它能讓我們輕松地讀取、寫入和操作 CSV 文件。下面我就來詳細(xì)給你講講 commons-csv 庫的使用方法。

1. 引入 commons-csv 庫

首先,你得把 commons-csv 庫添加到你的項目中。如果你使用的是 Maven 項目,就在 pom.xml 文件里添加以下依賴:

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

2. 讀取 CSV 文件

下面是使用 commons-csv 庫讀取 CSV 文件的示例代碼:

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
 
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
 
public class ReadCSVExample {
    public static void main(String[] args) {
        try {
            // 創(chuàng)建一個 Reader 對象,用于讀取 CSV 文件
            Reader in = new FileReader("example.csv");
 
            // 使用 CSVFormat.DEFAULT 來解析 CSV 文件
            CSVParser parser = new CSVParser(in, CSVFormat.DEFAULT);
 
            // 遍歷 CSV 文件的每一行記錄
            for (CSVRecord record : parser) {
                // 遍歷當(dāng)前記錄的每一個字段
                for (String field : record) {
                    System.out.print(field + "\t");
                }
                System.out.println();
            }
 
            // 關(guān)閉解析器
            parser.close();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("CSV 文件讀取失?。? + e.getMessage());
        }
    }
}

代碼解釋:

Reader in = new FileReader("example.csv");:創(chuàng)建一個 FileReader 對象,用于讀取 example.csv 文件。

CSVParser parser = new CSVParser(in, CSVFormat.DEFAULT);:使用 CSVFormat.DEFAULT 來解析 CSV 文件,創(chuàng)建一個 CSVParser 對象。

for (CSVRecord record : parser):遍歷 CSVParser 中的每一行記錄,CSVRecord 表示一行數(shù)據(jù)。

for (String field : record):遍歷當(dāng)前記錄的每一個字段,將其打印出來。

parser.close();:關(guān)閉 CSVParser,釋放資源。

3. 寫入 CSV 文件

接下來是使用 commons-csv 庫寫入 CSV 文件的示例代碼:

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
 
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
 
public class WriteCSVExample {
    public static void main(String[] args) {
        try {
            // 創(chuàng)建一個 Writer 對象,用于寫入 CSV 文件
            Writer out = new FileWriter("output.csv");
 
            // 使用 CSVFormat.DEFAULT 來創(chuàng)建 CSVPrinter 對象
            CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT);
 
            // 寫入表頭
            printer.printRecord("Name", "Age", "City");
 
            // 寫入數(shù)據(jù)行
            printer.printRecord("John", "25", "New York");
            printer.printRecord("Jane", "30", "Los Angeles");
 
            // 刷新并關(guān)閉 CSVPrinter
            printer.flush();
            printer.close();
 
            System.out.println("CSV 文件寫入成功!");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("CSV 文件寫入失?。? + e.getMessage());
        }
    }
}

代碼解釋:

Writer out = new FileWriter("output.csv");:創(chuàng)建一個 FileWriter 對象,用于寫入 output.csv 文件。

CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT);:使用 CSVFormat.DEFAULT 來創(chuàng)建 CSVPrinter 對象,用于寫入 CSV 文件。

printer.printRecord("Name", "Age", "City");:寫入 CSV 文件的表頭。

printer.printRecord("John", "25", "New York");:寫入一行數(shù)據(jù)。

printer.flush(); printer.close();:刷新緩沖區(qū)并關(guān)閉 CSVPrinter,確保數(shù)據(jù)寫入文件。

4. 自定義 CSV 格式

commons-csv 庫還允許我們自定義 CSV 文件的格式,比如指定分隔符、引號字符等。以下是一個示例:

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
 
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
 
public class CustomCSVFormatExample {
    public static void main(String[] args) {
        try {
            // 創(chuàng)建一個 Writer 對象,用于寫入 CSV 文件
            Writer out = new FileWriter("custom_output.csv");
 
            // 自定義 CSV 格式,使用分號作為分隔符
            CSVFormat customFormat = CSVFormat.DEFAULT.withDelimiter(';');
 
            // 使用自定義格式創(chuàng)建 CSVPrinter 對象
            CSVPrinter printer = new CSVPrinter(out, customFormat);
 
            // 寫入表頭
            printer.printRecord("Name", "Age", "City");
 
            // 寫入數(shù)據(jù)行
            printer.printRecord("John", "25", "New York");
            printer.printRecord("Jane", "30", "Los Angeles");
 
            // 刷新并關(guān)閉 CSVPrinter
            printer.flush();
            printer.close();
 
            System.out.println("自定義格式的 CSV 文件寫入成功!");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("CSV 文件寫入失?。? + e.getMessage());
        }
    }
}

代碼解釋:

CSVFormat customFormat = CSVFormat.DEFAULT.withDelimiter(';');:自定義 CSV 格式,使用分號作為分隔符。

CSVPrinter printer = new CSVPrinter(out, customFormat);:使用自定義格式創(chuàng)建 CSVPrinter 對象。

以上就是一文詳解Java如何使用commons-csv搞定CSV文件操作的詳細(xì)內(nèi)容,更多關(guān)于Java commons-csv操作CSV的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • MyBatis-Plus Sequence主鍵的實現(xiàn)

    MyBatis-Plus Sequence主鍵的實現(xiàn)

    這篇文章主要介紹了MyBatis-Plus Sequence主鍵的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • MacOS如何安裝配置多個JDK并切換使用詳解

    MacOS如何安裝配置多個JDK并切換使用詳解

    這篇文章主要介紹了如何在MacOS上安裝和配置多個JDK版本,通過配置環(huán)境變量來實現(xiàn),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-02-02
  • Springboot注解@Value讀取配置文件參數(shù)詳解

    Springboot注解@Value讀取配置文件參數(shù)詳解

    Spring Boot提供了靈活的配置文件讀取機(jī)制,主要有兩種方式,第一種是使用@Value注解直接在類屬性上讀取application.yml文件中的配置,這種方式簡單直接,但需要為每個配置項單獨設(shè)置屬性,第二種方式是通過@PropertySource注解讀取自定義的Properties文件
    2024-11-11
  • spring boot系列之集成測試(推薦)

    spring boot系列之集成測試(推薦)

    這篇文章主要介紹了spring boot系列集成測試,需要的朋友可以參考下
    2018-03-03
  • 淺談Mybatis SqlSession執(zhí)行流程

    淺談Mybatis SqlSession執(zhí)行流程

    本文主要介紹了淺談Mybatis SqlSession執(zhí)行流程,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • Java中Session的詳解

    Java中Session的詳解

    這篇文章主要介紹了了解java中的session的相關(guān)問題,什么是session,session怎么用等,具有一定參考價值,需要的朋友可以了解下。
    2021-10-10
  • Java如何自定義類數(shù)組的創(chuàng)建和初始化

    Java如何自定義類數(shù)組的創(chuàng)建和初始化

    這篇文章主要介紹了Java如何自定義類數(shù)組的創(chuàng)建和初始化,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • Mybatis實現(xiàn)批量操作8種小結(jié)

    Mybatis實現(xiàn)批量操作8種小結(jié)

    本文對Mybatis的五種批處理方式進(jìn)行了性能測試,包括批量新增和批量修改,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-10-10
  • Java動態(tài)調(diào)用類中方法代碼

    Java動態(tài)調(diào)用類中方法代碼

    這篇文章主要介紹了Java動態(tài)調(diào)用類中方法代碼,需要的朋友可以參考下
    2014-02-02
  • SpringBoot配置文件中數(shù)據(jù)庫密碼加密兩種方案(推薦)

    SpringBoot配置文件中數(shù)據(jù)庫密碼加密兩種方案(推薦)

    SpringBoot項目經(jīng)常將連接數(shù)據(jù)庫的密碼明文放在配置文件里,安全性就比較低一些,尤其在一些企業(yè)對安全性要求很高,因此我們就考慮如何對密碼進(jìn)行加密,文中給大家介紹加密的兩種方式,感興趣的朋友一起看看吧
    2019-10-10

最新評論