In this tutorial, we’ll look at different ways of writing into text files in Java. To learn how to read and write in binary files, we have the article Binary Files in Java.

The Writer Class

We can write data in the text file using different implementations of the Writer class, which represents an abstract class for writing to character streams.

The following list shows the implementations of the Writer class:

  • CharArrayWriter,
  • FilterWriter,
  • BufferedWriter,
  • OutputStreamWriter,
  • PipedWriter,
  • PrintWriter,
  • StringWriter

However, this article won’t cover all the implementations. Instead, we will focus on the most used ones. Each implementation provides the write() method, which can be used to write data in the file.

Using FileWriter Class

Let’s see how to write into text files using the FileWriter class.

The FileWriter class is used to write data represented by characters to the file. It extends the OutputStreamWriter class, which extends the Writer class.

We’ll use FileWriter to store a stream of characters in the file. For writing streams of bytes, we should consider using the FileOutputStream class instead. The article Binary Files in Java provides a detailed explanation of how to use it.

Let’s look at the example of how to write in the file using FileWriter class. Firstly, we’d need to create a new instance of the FileWriter class:

String filePath = "files/file_writer.txt"; 
FileWriter fileWriter = new FileWriter(filePath, true);

When creating a FileWriter instance, we need to pass the file path value. Additionally, we can pass a boolean value as a second argument. If the second argument is true, bytes will be written to the end of the file rather than the beginning. In other words, if true, we’ll append new values instead of writing over the existing ones.

The FileWriter will create a file instead of us if such a file is not already present.

Secondly, we’ll use the write() method in order to write characters in the file:

fileWriter.write("15646465464"); 
fileWriter.write(" "); 
fileWriter.write('j'); 
fileWriter.write(" "); 
fileWriter.write(String.valueOf(15)); 
fileWriter.write("\n");

We can pass char or String objects (including the char array).

Using BufferedWriter Class

The BufferedWriter class writes text to a character-output stream. Additionally, it buffers characters, which provides us with an efficient way of writing single characters, arrays, and strings.

Now, let’s see how to write data in a file using the BufferedWriter class. We’d need to pass an instance of a Writer as an argument when creating the BufferedWriter object. We can create an instance of the FileWriter class:

String filePath = "files/buffered_writer.txt"; 

FileWriter fileWriter = new FileWriter(filePath, true); 
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

Next, we can call its write() method and pass the String value:

bufferedWriter.write("ana-peterlic"); 
bufferedWriter.write(" "); 
bufferedWriter.write(String.valueOf(15)); 
bufferedWriter.newLine();

Moreover, the BufferedWriter class provides the newLine() method, which adds a line separator to the file.

Using PrintWriter Class

The PrintWrtier class prints formatted representations of objects to the text-output stream. In other words, it will write a string representation of data in the file. 

The PrintWrtier class contains the print() method, which can be used to write data to the file. Nevertheless, we can also use the write() method for the String data.

Let’s write data in the file using the PrintWrtier class:

String filePath = "java-core/files/dat/print_writer.txt";
try (FileWriter fileWriter = new FileWriter(filePath, true);
     PrintWriter printWriter = new PrintWriter(fileWriter))
{
    printWriter.write("test");
    printWriter.write("beenary");
    printWriter.print("value1");
} catch (Exception e)
{
    System.err.println("Error - " + e);
}

Conclusion

In this tutorial, we’ve explained how data can be stored and read from textual files.

As always, the entire code of this example can be found over on GitHub.

By Ana Peterlić

Ana is a Java Software Engineer with over 6 years of experience in designing and developing software solutions. She loves tutoring and helping others understand Java ecosystem.