最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

一文詳解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 庫就是一個非常強大且易用的工具,它能讓我們輕松地讀取、寫入和操作 CSV 文件。下面我就來詳細給你講講 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) {
                // 遍歷當前記錄的每一個字段
                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):遍歷當前記錄的每一個字段,將其打印出來。

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文件操作的詳細內(nèi)容,更多關(guān)于Java commons-csv操作CSV的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java中堆和棧的概念和區(qū)別

    Java中堆和棧的概念和區(qū)別

    Java的堆是一個運行時數(shù)據(jù)區(qū),類的對象從堆中分配空間。棧中主要存放一些基本數(shù)據(jù)類型的變量(byte,short,int,long,float,double,boolean,char)和對象的引用,這篇文章給大家詳細介紹java 堆和棧的概念和區(qū)別,一起看看吧
    2020-06-06
  • java 中break如何跳出外部循環(huán)

    java 中break如何跳出外部循環(huán)

    這篇文章主要介紹了java 中break如何跳出外部循環(huán),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • java Person,Student,GoodStudent 三個類的繼承、構(gòu)造函數(shù)的執(zhí)行

    java Person,Student,GoodStudent 三個類的繼承、構(gòu)造函數(shù)的執(zhí)行

    這篇文章主要介紹了java Person,Student,GoodStudent 三個類的繼承、構(gòu)造函數(shù)的執(zhí)行,需要的朋友可以參考下
    2017-02-02
  • SpringBoot整合RocketMQ極速實戰(zhàn)教程

    SpringBoot整合RocketMQ極速實戰(zhàn)教程

    本文詳細介紹了SpringBoot集成RocketMQ的全過程,涵蓋普通、廣播、順序、延遲、批量、過濾、事務(wù)等多種消息模式,通過自動裝配、全局配置、核心組件等介紹RocketMQ與SpringBoot的深入集成原理與實戰(zhàn)應(yīng)用,感興趣的朋友一起看看吧
    2026-06-06
  • MyBatis?在?Spring?Boot?中的實踐記錄

    MyBatis?在?Spring?Boot?中的實踐記錄

    MyBatis是持久層框架,簡化JDBC開發(fā),通過接口+XML/注解實現(xiàn)數(shù)據(jù)訪問,動態(tài)代理生成實現(xiàn)類,支持增刪改查及參數(shù)映射,配置數(shù)據(jù)庫連接與駝峰轉(zhuǎn)換,接下來通過本文給大家介紹破繭JDBC:MyBatis在Spring?Boot中的輕量實踐指南,感興趣的哦朋友一起看看吧
    2025-08-08
  • SpringBoot整合UEditor的示例代碼

    SpringBoot整合UEditor的示例代碼

    本篇文章主要介紹了SpringBoot整合UEditor的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • JAVA一個快速排序?qū)崿F(xiàn)代碼

    JAVA一個快速排序?qū)崿F(xiàn)代碼

    排序有哪幾種方法?請列舉。并用JAVA實現(xiàn)一個快速排序.,需要的朋友可以參考下
    2017-02-02
  • SpringBoot mail中文附件亂碼的解決方法

    SpringBoot mail中文附件亂碼的解決方法

    本篇文章主要介紹了SpringBoot mail中文附件亂碼的解決方法,非常具有實用價值,需要的朋友可以參考下
    2017-09-09
  • Java 異常的知識整理

    Java 異常的知識整理

    這篇文章主要介紹了Java 異常的知識整理的相關(guān)資料,需要的朋友可以參考下
    2017-07-07
  • 教你如何使用JAVA POI

    教你如何使用JAVA POI

    今天教大家怎么學(xué)習(xí)JAVA POI的用法,文中有非常詳細的代碼示例,對正在學(xué)習(xí)java的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05

最新評論

呼伦贝尔市| 沽源县| 虞城县| 焉耆| 鸡东县| 顺昌县| 旺苍县| 潜江市| 临颍县| 丹阳市| 中阳县| 石河子市| 平定县| 新昌县| 屯门区| 醴陵市| 肃宁县| 波密县| 盐边县| 北安市| 安龙县| 潜江市| 伊金霍洛旗| 库尔勒市| 惠东县| 五原县| 澄迈县| 阳曲县| 临沭县| 鹤庆县| 阳高县| 修水县| 九龙县| 九龙城区| 晋江市| 双峰县| 麻栗坡县| 乌兰浩特市| 开平市| 阆中市| 潞城市|