Reading a CSV File into an Array – 将CSV文件读入一个数组

最后修改: 2018年 10月 28日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Simply put, a CSV (Comma-Separated Values) file contains organized information separated by a comma delimiter.

简单地说,CSV(Comma-Separated Values)文件包含由逗号分隔的有序信息。

In this tutorial, we’ll look into different ways to read a CSV file into an array.

在本教程中,我们将研究将CSV文件读入数组的不同方法。

2. BufferedReader in java.io

2.java.io中的BufferedReader

First, we’ll read the records line by line using readLine() in BufferedReader.

首先,我们将在BufferedReader中使用readLine()逐行读取记录。

Then we’ll split the line into tokens based on the comma delimiter:

然后,我们将根据逗号分隔符把这一行分成若干个标记。

List<List<String>> records = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader("book.csv"))) {
    String line;
    while ((line = br.readLine()) != null) {
        String[] values = line.split(COMMA_DELIMITER);
        records.add(Arrays.asList(values));
    }
}

Note that more sophisticated CSVs (e.g., quoting or including commas as values) will not be parsed as intended with this approach.

请注意,更复杂的CSV(例如,引号或包括逗号作为值)将不会按照这种方法的意图进行解析。

3. Scanner in java.util

3.java.util中的Scanner

Next, we’re going to use a java.util.Scanner to run through the contents of the file and retrieve lines serially, one by one:

接下来,我们将使用java.util.Scanner来运行文件的内容,并一个一个地连续检索行。

List<List<String>> records = new ArrayList<>();
try (Scanner scanner = new Scanner(new File("book.csv"));) {
    while (scanner.hasNextLine()) {
        records.add(getRecordFromLine(scanner.nextLine()));
    }
}

Then we will parse the lines and store it into an array:

然后我们将解析这些行,并将其存储到一个数组中。

private List<String> getRecordFromLine(String line) {
    List<String> values = new ArrayList<String>();
    try (Scanner rowScanner = new Scanner(line)) {
        rowScanner.useDelimiter(COMMA_DELIMITER);
        while (rowScanner.hasNext()) {
            values.add(rowScanner.next());
        }
    }
    return values;
}

Like before, more sophisticated CSVs will not be parsed as intended with this approach.

像以前一样,更复杂的CSV将不能按照预期的方式解析,采用这种方法。

4. OpenCSV

4.呼叫中心

We can address more complex CSV files with OpenCSV.

我们可以用OpenCSV解决更复杂的CSV文件。

OpenCSV is a third-party library that provides an API to work with CSV files.

OpenCSV是一个第三方库,它提供了一个API来处理CSV文件。

We’ll use readNext() method in CSVReader to read the records in the file:

我们将使用CSVReader中的readNext()方法来读取文件中的记录。

List<List<String>> records = new ArrayList<List<String>>();
try (CSVReader csvReader = new CSVReader(new FileReader("book.csv"));) {
    String[] values = null;
    while ((values = csvReader.readNext()) != null) {
        records.add(Arrays.asList(values));
    }
}

To dig deeper and learn more about OpenCSV, check out our OpenCSV tutorial.

要深入了解OpenCSV,请查看我们的OpenCSV 教程

5. Conclusion

5.总结

In this quick article, we explored different ways to read CSV files into an array.

在这篇快速文章中,我们探讨了将CSV文件读入数组的不同方法。

As always, the full source code of the examples is available over on GitHub.

一如既往,这些示例的完整源代码可在GitHub上获得over