1. ホーム
  2. ジャワ

Java による csv ファイルの読み書き (オブジェクトデータへの自動抽出と変換)

2022-03-01 20:01:18
<パス

1. CSV入門

カンマ区切り値(CSV、区切り文字がカンマ以外でも良いので文字区切り値と呼ばれることもある)は、表形式のデータ(数値や文字)をプレーンテキストで保存したファイルである。CSVファイルは、ある種の改行で区切られた任意の数のレコードから構成されます。各レコードは、他の文字または文字列(多くの場合、カンマまたはタブ)で区切られたフィールドで構成されています。

2. Mavenの依存関係

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

/** * Write csv file (write at once, data should not be too big) * * @param objectList object * @param fileHeader header description * @param fileName File name (no .csv suffix) * @return File file * @throws BizException Exception */ public static File writeCsv(List objectList, String[] fileHeader, String fileName) { // explicitly configure the Header of the CSV file here, and then set the Header to be skipped (otherwise the header will be treated as a record when reading) CSVFormat format = CSVFormat.DEFAULT.withHeader(fileHeader).withRecordSeparator("\n"); // This is a locator to determine where in the records array the data for a field should be placed Map 4. Read csv The same can be automatically converted to objects based on annotations
/**
     * Read a csv file (not too big for a one-time read)
     *
     * @param filePath file path
     * @param headers csv column headers
     * @param tClass Return the type of the object
     * @return CSVRecord list
     * @throws BizException
     **/
    public static 
 List
 readCSV(String filePath, String[] headers, Class
 tClass) {
        // Create CSVFormat
        CSVFormat format = CSVFormat.DEFAULT.withHeader(headers);
        // Get the PropertyDescriptor of the object
        List
 tList = new ArrayList<>();
        try {
            Map
5. Testing
public static void main(String[] args) throws Exception {
        String[] fileHeader = {"name", "sex"};
        // test write
        List
 list = new ArrayList<>();
        for (int i = 0; i < 1000000; i++) {
            MsgResponse msgResponse = new MsgResponse();
            msgResponse.setCode("Name44444888");
            msgResponse.setMsg("Gender44444488");
            list.add(msgResponse);
        }
        long writeTimeStart = System.currentTimeMillis();
        CsvUtils.writeCsv(list, fileHeader, "d:\\workbook.csv");
        logger.info("writeTime:" + (System.currentTimeMillis() - writeTimeStart));
// test read
        long readTimeStart = System.currentTimeMillis();
        List
 m = CsvUtils.readCSV("d:\workbook.csv", fileHeader, MsgResponse.class);
        logger.info("ReadTime:" + (System.currentTimeMillis() - readTimeStart));
// for (MsgResponse msgResponse : m) {
// logger.info(msgResponse.getCode() + " " + msgResponse.getMsg());
// }
    }

Results

The generated csv may have encoding issues when opened in excel, just turn it, or use the text tool to open it 14:46:05.847 [main] INFO CsvUtils.class - write time: 593 14:46:07.237 [main] INFO CsvUtils.class - Read time: 1390 Process finished with exit code 0 The file is about 30M
csdn source code download && Baidu cloud download direct
The full source code except for the annotation class, which is a name field package com.store.common.utils; import com.google.common.collect.Maps; import com.store.common.annotation.CsvField; import com.store.common.exception.BizException; import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVPrinter; import org.apache.commons.csv.CSVRecord; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.beans; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.io.*; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Arrays; import java.util.List; Map; import java.util; /** * csv file read/write tool class * * @author by YangLD * @date 2018/7/10 */ public class CsvUtils { private static final Logger logger = LoggerFactory.getLogger("CsvUtils.class"); /** * Write csv file (write at once. Data should not be too large) * * @param objectList object * @param fileHeader header description * @param fileName File name (no .csv suffix) * @return File file * @throws BizException Exception */ public static File writeCsv(List objectList, String[] fileHeader, String fileName) { // explicitly configure the Header of the CSV file here, and then set the Header to be skipped (otherwise the header will be treated as a record when reading) CSVFormat format = CSVFormat.DEFAULT.withHeader(fileHeader).withRecordSeparator("\n"); // This is a locator to determine where in the records array the data for a field should be placed Map List readCSV(String filePath, String[] headers, Class tClass) { // Create CSVFormat CSVFormat format = CSVFormat.DEFAULT.withHeader(headers); // Get the PropertyDescriptor of the object List tList = new ArrayList<>(); try { Map list = new ArrayList<>(); for (int i = 0; i < 1000; i++) { list.add(new TargetObject("Name"+i,""+i+100)); } long writeTimeStart = System.currentTimeMillis(); CsvUtils.writeCsv(list, fileHeader, "d:\\write.csv"); logger.info("writeTime:" + (System.currentTimeMillis() - writeTimeStart)); // test read long readTimeStart = System.currentTimeMillis(); List targetObjectList = CsvUtils.readCSV("d:\write.csv", fileHeader, TargetObject.class); logger.info("ReadTime:" + (System.currentTimeMillis() - readTimeStart)); for (TargetObject targetObject : targetObjectList) { System.out.println(targetObject.getName() + " : " + targetObject.getAge()); } } /** targetObject */ public static class TargetObject{ public TargetObject() { } public TargetObject(String name, String age) { this.name = name; this.age = age; } @CsvField(name = "name") private String name; @CsvField(name = "age") private String age; public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } } }