1. Overview
1.概述
In this tutorial, we’ll learn and understand the FileWriter class present in the java.io package.
在本教程中,我们将学习并理解存在于java.io包中的FileWriter类。
2. FileWriter
2.FileWriter
FileWriter is a specialized OutputStreamWriter for writing character files. It doesn’t expose any new operations but works with the operations inherited from the OutputStreamWriter and Writer classes.
FileWriter是一个专门的OutputStreamWriter用于编写字符文件。它没有暴露任何新的操作,而是与从OutputStreamWriter和Writer类继承的操作一起工作。
Until Java 11, the FileWriter worked with the default character encoding and default byte buffer size. However, Java 11 introduced four new constructors that accept a Charset, thereby allowing user-specified Charset. Unfortunately, we still cannot modify the byte buffer size, and it’s set to 8192.
在 Java 11 之前,FileWriter 使用默认的字符编码和默认的字节缓冲区大小。然而,Java 11 引入了四个新的构造函数,接受Charset,从而允许用户指定Charset。不幸的是,我们仍然不能修改字节缓冲区的大小,它被设置为8192。
2.1. Instantiating the FileWriter
2.1.实例化FileWriter
There are five constructors in the FileWriter class if we’re using a Java version before Java 11.
如果我们使用Java 11之前的Java版本,FileWriter类中有五个构造函数。
Let’s have a glance at various constructors:
让我们看一下各种构造函数。
public FileWriter(String fileName) throws IOException {
super(new FileOutputStream(fileName));
}
public FileWriter(String fileName, boolean append) throws IOException {
super(new FileOutputStream(fileName, append));
}
public FileWriter(File file) throws IOException {
super(new FileOutputStream(file));
}
public FileWriter(File file, boolean append) throws IOException {
super(new FileOutputStream(file, append));
}
public FileWriter(FileDescriptor fd) {
super(new FileOutputStream(fd));
}
Java 11 introduced four additional constructors:
Java 11引入了四个额外的构造函数。
public FileWriter(String fileName, Charset charset) throws IOException {
super(new FileOutputStream(fileName), charset);
}
public FileWriter(String fileName, Charset charset, boolean append) throws IOException {
super(new FileOutputStream(fileName, append), charset);
}
public FileWriter(File file, Charset charset) throws IOException {
super(new FileOutputStream(file), charset);
}
public FileWriter(File file, Charset charset, boolean append) throws IOException {
super(new FileOutputStream(file, append), charset);
}
2.2. Writing a String to a File
2.2.将一个字符串写入文件
Let’s now use one of the FileWriter constructors to create an instance of FileWriter and then write to a file:
现在让我们使用其中一个FileWriter构造函数来创建一个FileWriter的实例,然后写到一个文件。
try (FileWriter fileWriter = new FileWriter("src/test/resources/FileWriterTest.txt")) {
fileWriter.write("Hello Folks!");
}
We’ve used the single argument constructor of the FileWriter that accepts a file name. We then use the write(String str) operation inherited from the Writer class. Since the FileWriter is AutoCloseable, we’ve used try-with-resources so that we don’t have to close the FileWriter explicitly.
我们使用了FileWriter的单参数构造器,它接受了一个文件名。 然后我们使用从Writer类继承的write(String str)操作。由于FileWriter是AutoCloseable,我们使用了try-with-resources,这样我们就不必明确关闭FileWriter。
On executing the above code, the String will be written to the specified file:
在执行上述代码时,字符串将被写入指定文件。
Hello Folks!
The FileWriter does not guarantee whether the FileWriterTest.txt file will be available or be created. It is dependent on the underlying platform.
FileWriter并不保证FileWriterTest.txt文件是否可用或被创建。它取决于底层平台。
We must also make a note that certain platforms may allow only a single FileWriter instance to open the file. In that case, the other constructors of the FileWriter class will fail if the file involved is already open.
我们还必须注意,某些平台可能只允许一个FileWriter实例来打开文件。在这种情况下,如果所涉及的文件已经打开,FileWriter类的其他构造函数将会失败。
2.3. Appending a String to a File
2.3.将一个字符串添加到一个文件
We often need to append data to the existing contents of a file. Let’s now see an example of a FileWriter that supports appending:
我们经常需要将数据追加到一个文件的现有内容中。现在让我们看看一个支持追加的FileWriter的例子。
try (FileWriter fileWriter = new FileWriter("src/test/resources/FileWriterTest.txt", true)) {
fileWriter.write("Hello Folks Again!");
}
As we can see, we’ve used the two-argument constructor that accepts a file name and a boolean flag append. Passing the flag append as true creates a FileWriter that allows us to append text to existing contents of a file.
正如我们所见,我们使用了双参数构造函数,它接受一个文件名和一个boolean标志append。将标志append设为true,可以创建一个FileWriter,允许我们将文本追加到文件的现有内容中。
On executing the code, we’ll have the String appended to the existing contents of the specified file:
在执行该代码时,我们将把String追加到指定文件的现有内容中。
Hello Folks!Hello Folks Again!
3. Conclusion
3.结论
In this article, we learned about the convenience class FileWriter and a couple of ways in which the FileWriter can be created. We then used it to write data to a file.
在这篇文章中,我们了解了便利类FileWriter以及创建FileWriter的几种方法。然后我们用它把数据写到了一个文件中。
As always, the complete source code for the tutorial is available over on GitHub.
一如既往,该教程的完整源代码可在GitHub上获取。