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

Java使用Apache Commons高效處理CSV文件的操作指南

 更新時(shí)間:2025年03月07日 09:49:13   作者:拾荒的小海螺  
在 Java 開發(fā)中,CSV(Comma-Separated Values,逗號(hào)分隔值)是一種常見的數(shù)據(jù)存儲(chǔ)格式,廣泛用于數(shù)據(jù)交換和簡(jiǎn)單的存儲(chǔ)任務(wù),本文將介紹Java使用Apache Commons高效處理CSV文件的操作指南,需要的朋友可以參考下

1、簡(jiǎn)述

在 Java 開發(fā)中,CSV(Comma-Separated Values,逗號(hào)分隔值)是一種常見的數(shù)據(jù)存儲(chǔ)格式,廣泛用于數(shù)據(jù)交換和簡(jiǎn)單的存儲(chǔ)任務(wù)。Apache Commons CSV 是 Apache 提供的一個(gè)輕量級(jí)庫(kù),專注于簡(jiǎn)化 CSV 文件的解析和生成,支持多種 CSV 格式,如 Excel、RFC 4180、MySQL 等。

本文將介紹 Commons CSV 的核心功能,并通過多個(gè)詳細(xì)的使用示例展示其在 CSV 文件解析和生成中的強(qiáng)大功能。

2、為什么選擇 Commons CSV?

  • 輕量級(jí):無需龐大的依賴,功能集中。
  • 支持多種格式:兼容 Excel、RFC 4180、Tab 分隔等格式。
  • 簡(jiǎn)單易用:API 設(shè)計(jì)清晰,易于上手。
  • 靈活性高:支持自定義分隔符、自定義換行符等多種配置。

在使用 Commons CSV之前,需要添加其依賴。以下是 Commons CSV 的 Maven 依賴:

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

3、使用樣例

Spring Boot 集成 Commons CSV 常見的使用樣例,以下舉例供參考:

3.1 寫入 CSV 文件

Commons CSV 同樣支持輕松生成 CSV 文件:

package com.lm.csv.example;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;

import java.io.FileWriter;
import java.io.IOException;

public class CsvWriterExample {
    public static void main(String[] args) throws IOException {
        // 創(chuàng)建 CSV 文件
        try (FileWriter writer = new FileWriter("e:\\csv\\output.csv");
             CSVPrinter printer = new CSVPrinter(writer, CSVFormat.DEFAULT
                     .withHeader("ID", "Name", "Age", "Email"))) {

            printer.printRecord("1", "Alice", "25", "alice@example.com");
            printer.printRecord("2", "Bob", "30", "bob@example.com");
            printer.printRecord("3", "Charlie", "35", "charlie@example.com");
        }
    }
}

3.2 使用自定義分隔符

如果需要自定義分隔符(例如分號(hào) :),可以通過配置實(shí)現(xiàn):

package com.lm.csv.example;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;

import java.io.FileWriter;

public class CustomDelimiterExample {
    public static void main(String[] args) throws Exception {
        try (FileWriter writer = new FileWriter("e:\\csv\\custom_delimiter.csv");
             CSVPrinter printer = new CSVPrinter(writer, CSVFormat.DEFAULT
                     .withHeader("ID", "Name", "Age", "Email")
                     .withDelimiter(':'))) {

            printer.printRecord("1", "Diana", "40", "diana@example.com");
            printer.printRecord("2", "Eve", "22", "eve@example.com");
        }
    }
}

3.3 解析嵌套引號(hào)或特殊字符

CSV 文件中可能包含嵌套引號(hào)或特殊字符(如換行符),Commons CSV 能輕松解析:

package com.lm.csv.example;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;

import java.io.StringReader;

public class SpecialCharacterExample {
    public static void main(String[] args) throws Exception {
        String csvData = "ID,Name,Notes\n" +
                "1,\"John\",\"Loves coding\nand teaching\"\n" +
                "2,\"Jane\",\"Enjoys reading\"";

        try (CSVParser parser = CSVFormat.DEFAULT
                .withFirstRecordAsHeader()
                .parse(new StringReader(csvData))) {

            parser.forEach(record -> {
                String id = record.get("ID");
                String name = record.get("Name");
                String notes = record.get("Notes");

                System.out.printf("ID: %s, Name: %s, Notes: %s%n", id, name, notes);
            });
        }
    }
}

3.4 使用枚舉映射字段

對(duì)于字段定義明確的 CSV 文件,可以使用枚舉來避免硬編碼字段名稱:

package com.lm.csv.example;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;

import java.io.FileReader;

public class EnumFieldExample {
    enum Header {
        ID, Name, Age, Email
    }

    public static void main(String[] args) throws Exception {
        try (FileReader reader = new FileReader("e:\\csv\\output.csv");
             CSVParser parser = CSVFormat.DEFAULT
                     .withFirstRecordAsHeader()
                     .parse(reader)) {

            for (CSVRecord record : parser) {
                String id = record.get(Header.ID);
                String name = record.get(Header.Name);
                String age = record.get(Header.Age);
                String email = record.get(Header.Email);

                System.out.printf("ID: %s, Name: %s, Age: %s, Email: %s%n", id, name, age, email);
            }
        }
    }
}

4、總結(jié)

Apache Commons CSV 是處理 CSV 文件的高效工具,無論是解析復(fù)雜的 CSV 數(shù)據(jù)還是生成自定義格式的 CSV 文件,都能提供簡(jiǎn)潔高效的解決方案。

優(yōu)點(diǎn):

  • 輕量級(jí)且易于使用。
  • 豐富的功能支持,如自定義分隔符、多格式支持。
  • 提供全面的 CSV 文件讀取和寫入功能。

適用場(chǎng)景:

  • 數(shù)據(jù)導(dǎo)入和導(dǎo)出。
  • 數(shù)據(jù)轉(zhuǎn)換和清洗。
  • 作為應(yīng)用程序中的輕量級(jí)數(shù)據(jù)庫(kù)。
  • 通過本文的示例,希望你能夠快速掌握 Commons CSV 的使用方法,并靈活應(yīng)用于實(shí)際項(xiàng)目中!

以上就是Java使用Apache Commons高效處理CSV文件的操作指南的詳細(xì)內(nèi)容,更多關(guān)于Java Apache Commons處理CSV的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Spring實(shí)例化bean的方式代碼詳解

    Spring實(shí)例化bean的方式代碼詳解

    這篇文章主要介紹了Spring實(shí)例化bean的方式代碼詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • spring boot讀取Excel操作示例

    spring boot讀取Excel操作示例

    這篇文章主要介紹了spring boot讀取Excel操作,結(jié)合實(shí)例形式詳細(xì)分析了spring boot解析、讀取Excel相關(guān)操作技巧,需要的朋友可以參考下
    2019-11-11
  • Java 如何實(shí)現(xiàn)時(shí)間控制

    Java 如何實(shí)現(xiàn)時(shí)間控制

    這篇文章主要向大家介紹得是Java 如何實(shí)現(xiàn)時(shí)間控制,文章珠岙舉例說明該內(nèi)容,感興趣得小伙伴可以跟小編一起學(xué)習(xí)下面文章內(nèi)容
    2021-10-10
  • spring boot mybatis枚舉映射示例代碼

    spring boot mybatis枚舉映射示例代碼

    這篇文章主要給大家介紹了關(guān)于spring boot mybatis枚舉映射的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • minio的下載和springboot整合minio使用方法

    minio的下載和springboot整合minio使用方法

    本文介紹了如何通過Docker拉取MinIO鏡像,并創(chuàng)建MinIO容器的過程,首先,需要在本地創(chuàng)建/data和/conf兩個(gè)目錄用于掛載MinIO的數(shù)據(jù)和配置文件,接下來,通過docker?run命令啟動(dòng)容器,設(shè)置MinIO的訪問端口、用戶名、密碼等信息,感興趣的朋友一起看看吧
    2024-09-09
  • 如何使用Spring Boot實(shí)現(xiàn)接口時(shí)間戳鑒權(quán)

    如何使用Spring Boot實(shí)現(xiàn)接口時(shí)間戳鑒權(quán)

    這篇文章主要為大家詳細(xì)介紹了如何使用Spring Boot實(shí)現(xiàn)接口時(shí)間戳鑒權(quán),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的小伙伴要了解下
    2025-06-06
  • Springcloud Eureka配置及集群代碼實(shí)例

    Springcloud Eureka配置及集群代碼實(shí)例

    這篇文章主要介紹了Springcloud Eureka配置及集群代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • SpringBoot參數(shù)校驗(yàn)及原理全面解析

    SpringBoot參數(shù)校驗(yàn)及原理全面解析

    文章介紹了SpringBoot中使用@Validated和@Valid注解進(jìn)行參數(shù)校驗(yàn)的方法,包括基本用法和進(jìn)階用法,如自定義驗(yàn)證注解、多屬性聯(lián)合校驗(yàn)和嵌套校驗(yàn),并簡(jiǎn)要介紹了實(shí)現(xiàn)原理
    2024-11-11
  • 輕松掌握java外觀模式

    輕松掌握java外觀模式

    這篇文章主要幫助大家輕松掌握java外觀模式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-09-09
  • mybatis plus開發(fā)過程中遇到的問題記錄及解決

    mybatis plus開發(fā)過程中遇到的問題記錄及解決

    這篇文章主要介紹了mybatis plus開發(fā)過程中遇到的問題記錄及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07

最新評(píng)論

泌阳县| 来宾市| 连城县| 金溪县| 万山特区| 时尚| 皮山县| 青川县| 泗阳县| 延长县| 突泉县| 大理市| 祁连县| 双城市| 万载县| 临清市| 民乐县| 宜丰县| 十堰市| 泗水县| 西华县| 平江县| 陇西县| 临沧市| 从江县| 易门县| 壤塘县| 双桥区| 仪征市| 治多县| 周至县| 祁门县| 双鸭山市| 密云县| 开鲁县| 永丰县| 荆门市| 海城市| 富宁县| 玉环县| 乐安县|