1. Overview
1.概述
In this quick tutorial, we’ll learn how to write to a CSV file using Java. CSV stands for Comma-Separated-Values, and it’s a common format for bulk data transfers between systems.
在这个快速教程中,我们将学习如何使用Java写到CSV文件。CSV是Comma-Separated-Values的缩写,它是系统间批量数据传输的一种常见格式。
To write our CSV file, we’ll be using classes in the java.io package.
为了编写我们的CSV文件,我们将使用java.io包中的类。
We’ll talk about special characters and how to handle them. We’ll be targeting our output file to open in Microsoft Excel and Google Sheets.
我们将讨论特殊字符以及如何处理它们。我们将针对我们的输出文件在Microsoft Excel和Google Sheets中打开。
After our Java example, we’ll take a brief look at some available third-party libraries for working with CSV files.
在我们的Java例子之后,我们将简要地看一下一些可用的第三方库,用于处理CSV文件。
2. Writing With PrintWriter
2.用PrintWriter进行写作
We’re going to use a PrintWriter for writing our CSV file. For a more detailed look at using java.io to write to a file, see our article on writing to files.
我们将使用一个PrintWriter来写我们的CSV文件。关于使用java.io向文件写入的更多细节,请参阅我们的向文件写入的文章。
2.1. Writing the CSV
2.1.编写CSV
First, let’s create a method for formatting a single line of data represented as an array of Strings:
首先,让我们创建一个方法来格式化以Strings数组表示的单行数据。
public String convertToCSV(String[] data) {
return Stream.of(data)
.map(this::escapeSpecialCharacters)
.collect(Collectors.joining(","));
}
Before we call this method though, let’s build up some example data:
不过在我们调用这个方法之前,让我们先建立一些实例数据。
List<String[]> dataLines = new ArrayList<>();
dataLines.add(new String[]
{ "John", "Doe", "38", "Comment Data\nAnother line of comment data" });
dataLines.add(new String[]
{ "Jane", "Doe, Jr.", "19", "She said \"I'm being quoted\"" });
With that data in hand, let’s convert each row with convertToCSV, and write it to a file:
有了这些数据,让我们用convertToCSV,转换每一行,并将其写入一个文件中。
public void givenDataArray_whenConvertToCSV_thenOutputCreated() throws IOException {
File csvOutputFile = new File(CSV_FILE_NAME);
try (PrintWriter pw = new PrintWriter(csvOutputFile)) {
dataLines.stream()
.map(this::convertToCSV)
.forEach(pw::println);
}
assertTrue(csvOutputFile.exists());
}
2.2. Handling Special Characters
2.2.处理特殊字符
In a CSV file, certain characters are problematic, and as developers, we rarely have total control over the quality of our data. So now let’s look at how to handle special characters.
在CSV文件中,某些字符是有问题的,而作为开发者,我们很少能完全控制数据的质量。所以现在让我们来看看如何处理特殊字符。
For our example, we’ll focus on commas, quotes, and new lines. Fields containing commas or quotes will be surrounded by double quotes, and double quotes will be escaped with double quotes. We’ll eliminate new lines and replace them each with white space.
对于我们的例子,我们将专注于逗号、引号和新行。包含逗号或引号的字段将被双引号包围,而双引号将被双引号转义。我们将消除新行,并将其分别替换为空白。
Problematic characters and how they should be handled may vary with the use case.
有问题的字符以及如何处理它们可能因使用情况而异。
Our convertToCSV method calls the escapeSpecialCharacters method on each piece of data as it’s building up a String.
我们的convertToCSV方法在建立String.时,对每一个数据调用escapeSpecialCharacters方法。
Let’s implement our escapeSpecialCharacters method now:
现在我们来实现我们的escapeSpecialCharacters方法。
public String escapeSpecialCharacters(String data) {
String escapedData = data.replaceAll("\\R", " ");
if (data.contains(",") || data.contains("\"") || data.contains("'")) {
data = data.replace("\"", "\"\"");
escapedData = "\"" + data + "\"";
}
return escapedData;
}
3. Third-Party Libraries
3.第三方图书馆
As we saw with our example, writing a CSV file can become complicated when we start thinking about special characters and how to handle them.
正如我们所看到的例子,当我们开始考虑特殊字符和如何处理它们时,编写CSV文件会变得复杂。
Luckily for us, there are many third-party libraries available for working with CSV files, and many of them handle these special characters and other exceptional cases that may occur.
幸运的是,有许多第三方库可用于处理CSV文件,其中许多库可以处理这些特殊字符和其他可能出现的特殊情况。
Let’s take a look at a few of them:
让我们来看看其中的几个例子。
- Apache Commons CSV: Apache’s CSV offering for working with CSV Files
- Open CSV: Another popular and actively-maintained CSV library
- Flatpack: An open-source CSV library being actively developed
- CSVeed: Open-source and actively-maintained
4. Conclusion
4.总结
In this brief article, we discussed how to write a CSV file using Java’s PrintWriter class. Next, we discussed and handled special characters in the data being output.
在这篇简短的文章中,我们讨论了如何使用Java的PrintWriter类编写一个CSV文件。接下来,我们讨论并处理了被输出数据中的特殊字符。
After our plain Java example, we looked at an overview of available third-party libraries.
在我们的普通Java例子之后,我们看了一下可用的第三方库的概述。
The example code is available over on GitHub.
该示例代码可在GitHub上获得。。