1. Introduction
1.介绍
In this quick tutorial, we’ll see how we use Java to append data to the content of a file – in a few simple ways.
在这个快速教程中,我们将看到如何使用Java将数据追加到文件的内容中–有几种简单的方法。
Let’s start with how we can do this using core Java’s FileWriter.
让我们先来看看如何使用核心Java的FileWriter.来实现这一目标。
2. Using FileWriter
2.使用FileWriter
Here’s a simple test – reading an existing file, appending some text, and then making sure that got appended correctly:
这里有一个简单的测试–读取一个现有的文件,添加一些文本,然后确保这些文本被正确添加。
@Test
public void whenAppendToFileUsingFileWriter_thenCorrect()
throws IOException {
FileWriter fw = new FileWriter(fileName, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("Spain");
bw.newLine();
bw.close();
assertThat(getStringFromInputStream(
new FileInputStream(fileName)))
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
}
Note that FileWriter’s constructor accepts a boolean marking if we want to append data to an existing file.
请注意,FileWriter的构造函数接受一个boolean标记,如果我们想将数据追加到一个现有的文件。
If we set it to false, then the existing content will be replaced.
如果我们将其设置为false,那么现有的内容将被替换。
3. Using FileOutputStream
3.使用FileOutputStream
Next – let’s see how we can do the same operation – using FileOutputStream:
接下来–让我们看看如何做同样的操作–使用FileOutputStream。
@Test
public void whenAppendToFileUsingFileOutputStream_thenCorrect()
throws Exception {
FileOutputStream fos = new FileOutputStream(fileName, true);
fos.write("Spain\r\n".getBytes());
fos.close();
assertThat(StreamUtils.getStringFromInputStream(
new FileInputStream(fileName)))
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
}
Similarly, the FileOutputStream constructor accepts a boolean that should be set to true to mark that we want to append data to an existing file.
同样,FileOutputStream构造函数接受一个布尔值,该布尔值应设置为true,以标记我们要将数据追加到一个现有文件中。
4. Using java.nio.file
4.使用java.nio.file
Next – we can also append content to files using functionality in java.nio.file – which was introduced in JDK 7:
接下来–我们还可以使用java.nio.file中的功能向文件追加内容–这是在JDK 7中引入的。
@Test
public void whenAppendToFileUsingFiles_thenCorrect()
throws IOException {
String contentToAppend = "Spain\r\n";
Files.write(
Paths.get(fileName),
contentToAppend.getBytes(),
StandardOpenOption.APPEND);
assertThat(StreamUtils.getStringFromInputStream(
new FileInputStream(fileName)))
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
}
5. Using Guava
5.使用Guava
To start using Guava, we need to add its dependency to our pom.xml:
要开始使用Guava,我们需要把它的依赖性添加到我们的pom.xml中。
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>31.0.1-jre</version>
</dependency>
Now, let’s see how we can start using Guava to append content to an existing file:
现在,让我们看看如何开始使用Guava将内容追加到一个现有的文件。
@Test
public void whenAppendToFileUsingFileWriter_thenCorrect()
throws IOException {
File file = new File(fileName);
CharSink chs = Files.asCharSink(
file, Charsets.UTF_8, FileWriteMode.APPEND);
chs.write("Spain\r\n");
assertThat(StreamUtils.getStringFromInputStream(
new FileInputStream(fileName)))
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
}
6. Using Apache Commons IO FileUtils
6.使用Apache Commons IO FileUtils
Finally – let’s see how we can append content to an existing file using Apache Commons IO FileUtils.
最后–让我们看看如何使用Apache Commons IO FileUtils将内容追加到一个现有的文件。
First, let’s add the Apache Commons IO dependency to our pom.xml:
首先,让我们把Apache Commons IO的依赖关系添加到我们的pom.xml。
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
Now, let’s see a quick example that demonstrates appending content to an existing file using FileUtils:
现在,让我们看一个快速的例子,演示如何使用FileUtils将内容追加到一个现有的文件。
@Test
public void whenAppendToFileUsingFiles_thenCorrect()
throws IOException {
File file = new File(fileName);
FileUtils.writeStringToFile(
file, "Spain\r\n", StandardCharsets.UTF_8, true);
assertThat(StreamUtils.getStringFromInputStream(
new FileInputStream(fileName)))
.isEqualTo("UK\r\n" + "US\r\n" + "Germany\r\n" + "Spain\r\n");
}
7. Conclusion
7.结论
In this article, we’ve seen how we can append content in multiple ways.
在这篇文章中,我们已经看到我们如何以多种方式追加内容。
The full implementation of this tutorial can be found over on GitHub.
本教程的完整实现可以在GitHub上找到over。