1. Overview
1.概述
Apache Commons CSV library has many useful features for creating and reading CSV files.
Apache Commons CSV库具有许多创建和读取CSV文件的有用功能。
In this quick tutorial, we’ll see how to utilize this library by showing a simple example.
在这个快速教程中,我们将通过展示一个简单的例子来了解如何利用这个库。
2. Maven Dependency
2.Maven的依赖性
To start, we will import the latest version of this library using Maven:
首先,我们将使用Maven导入该库的最新版本。
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.4</version>
</dependency>
To check for the most recent version of this library – go here.
要检查这个库的最新版本 – 到这里。
3. Reading a CSV File
3.读取CSV文件
Consider the following CSV file called book.csv containing the attributes of a book:
考虑以下名为book.csv的CSV文件,包含一本书的属性。
author,title
Dan Simmons,Hyperion
Douglas Adams,The Hitchhiker's Guide to the Galaxy
Let’s see how we can read it:
让我们看看如何读懂它。
Map<String, String> AUTHOR_BOOK_MAP = new HashMap<>() {
{
put("Dan Simmons", "Hyperion");
put("Douglas Adams", "The Hitchhiker's Guide to the Galaxy");
}
});
String[] HEADERS = { "author", "title"};
@Test
public void givenCSVFile_whenRead_thenContentsAsExpected() throws IOException {
Reader in = new FileReader("book.csv");
Iterable<CSVRecord> records = CSVFormat.DEFAULT
.withHeader(HEADERS)
.withFirstRecordAsHeader()
.parse(in);
for (CSVRecord record : records) {
String author = record.get("author");
String title = record.get("title");
assertEquals(AUTHOR_BOOK_MAP.get(author), title);
}
}
We are reading the records of a CSV file after skipping the first line as it is the header.
我们在读取CSV文件的记录时,要跳过第一行,因为它是标题。
There are different types of CSVFormat specifying the format of the CSV file, an example of which you can see in the next paragraph.
有不同类型的CSVFormat指定CSV文件的格式,在下一段中你可以看到一个例子。
4. Creating a CSV File
4.创建一个CSV文件
Let’s see how we can create the same CSV file as above:
让我们看看如何创建和上面一样的CSV文件。
public void createCSVFile() throws IOException {
FileWriter out = new FileWriter("book_new.csv");
try (CSVPrinter printer = new CSVPrinter(out, CSVFormat.DEFAULT
.withHeader(HEADERS))) {
AUTHOR_BOOK_MAP.forEach((author, title) -> {
printer.printRecord(author, title);
});
}
}
The new CSV file will be created with the appropriate headers because we have specified them in our CSVFormat declaration.
新的CSV文件将被创建,并带有适当的标题,因为我们已经在CSVFormat声明中指定了它们。
5. Headers & Reading Columns
5.页眉&阅读栏
There are different ways to read and write headers. Similarly, there are different ways to read column values.
有不同的方法来读和写标题。同样,也有不同的方式来读取列值。
Let’s go through them one by one:
让我们一个一个地看下去。
5.1. Accessing Columns by Index
5.1.按索引访问列
This is the most basic way to read column values. This can be used when the headers for the CSV files are not known:
这是最基本的读取列值的方法。当不知道CSV文件的标题时,可以使用这种方法。
Reader in = new FileReader("book.csv");
Iterable<CSVRecord> records = CSVFormat.DEFAULT.parse(in);
for (CSVRecord record : records) {
String columnOne = record.get(0);
String columnTwo = record.get(1);
}
5.2. Accessing Columns by Predefined Headers
5.2.通过预定义标题访问列
This is a more intuitive way to access columns when compared to accessing by indices:
与通过指数访问相比,这是一种更直观的访问列的方式。
Iterable<CSVRecord> records = CSVFormat.DEFAULT
.withHeader("author", "title").parse(in);
for (CSVRecord record : records) {
String author = record.get("author");
String title = record.get("title");
}
5.3. Using Enums as Headers
5.3.将枚举作为标头
Using Strings for accessing column values can be error-prone. Using Enums instead of Strings will make the code more standardized and easier to understand:
使用字符串来访问列值可能会产生错误。使用Enums而不是Strings将使代码更加标准化,更容易理解。
enum BookHeaders {
author, title
}
Iterable<CSVRecord> records = CSVFormat.DEFAULT
.withHeader(BookHeaders.class).parse(in);
for (CSVRecord record : records) {
String author = record.get(BookHeaders.author);
String title = record.get(BookHeaders.title);
}
5.4. Skipping the Header Line
5.4.跳过标题行
Usually, CSV files contain headers in the first line. Hence, in most cases, it is safe to skip it and start reading from the second row.
通常情况下,CSV文件的第一行包含标题。因此,在大多数情况下,跳过它并从第二行开始阅读是安全的。
This will autodetect headers access column values:
这将自动检测页眉访问列值。
Iterable<CSVRecord> records = CSVFormat.DEFAULT
.withFirstRowAsHeader().parse(in);
for (CSVRecord record : records) {
String author = record.get("author");
String title = record.get("title");
}
5.5. Creating a File With Headers
5.5.创建一个带有页眉的文件
Similarly, we can create a CSV file with the first line containing the headers:
同样地,我们可以创建一个CSV文件,第一行包含标题。
FileWriter out = new FileWriter("book_new.csv");
CSVPrinter printer = CSVFormat.DEFAULT
.withHeader("author", "title").print(out);
6. Conclusion
6.结论
We presented the use of Apache’s Commons CSV library through a simple example. You can read more about the library here.
我们通过一个简单的例子介绍了Apache的Commons CSV库的使用。你可以在这里阅读关于该库的更多信息。
The code for this article is available over on Github.
本文的代码可在Github上获得over。