Tech and Media Labs
This site uses cookies to improve the user experience.




Java IO: FileWriter

Jakob Jenkov
Last update: 2015-09-06

The Java FileWriter class (java.io.FileWriter) makes it possible to write characters to a file. In that respect it works much like the FileOutputStream except that a FileOutputStream is byte based, whereas a FileWriter is character based. The FileWriter is intended to write text, in other words. One character may correspond to one or more bytes, depending on the character encoding scheme in use.

FileWriter Example

Here is a simple Java FileWriter example:

Writer fileWriter = new FileWriter("data\\filewriter.txt");

fileWriter.write("data 1");
fileWriter.write("data 2");
fileWriter.write("data 3");

fileWriter.close();

Note: The proper exception handling has been skipped here for the sake of clarity. To learn more about correct exception handling, go to Java IO Exception Handling.

The FileWriter has other constructors too, letting you specify the file to write to in different ways. Look in the official JavaDoc for more detailed info.

Overwriting vs. Appending the File

When you create a Java FileWriter you can decide if you want to overwrite any existing file with the same name, or if you want to append to any existing file. You decide that by choosing what FileWriter constructor you use.

The FileWriter constructor taking just one parameter, the file name, will overwrite any existing file:

Writer fileWriter = new FileWriter("c:\\data\\output.txt");

FileWriter has a constructor that takes 2 parameters too: The file name and a boolean. The boolean indicates whether to append or overwrite an existing file. Here are two Java FileWriter examples showing that:

Writer fileWriter = new FileWriter("c:\\data\\output.txt", true);  //appends to file

Writer fileWriter = new FileWriter("c:\\data\\output.txt", false); //overwrites file

FileWriter Character Encoding

The FileWriter assumes that you want to encode the bytes to the file using the default character encoding for the computer your application is running on. This may not always be what you want, and you cannot change it!

If you want to specify a different character encoding scheme, don't use a FileWriter. Use an OutputStreamWriter on a FileOutputStream instead. The OutputStreamWriter lets you specify the character encoding scheme to use when writing bytes to the underlying file.

Closing a FileWriter

When you are finished writing characters to a Java FileWriter you should remember to close it. Closing a FileWriter is done by calling its close() method. Here is how closing a Java FileWriter looks:

fileWriter.close();

You can also use the try-with-resources construct introduced in Java 7. Here is how to use and close a FileWriter looks with the try-with-resources construct:

try(FileWriter fileWriter =
    new FileWriter("data\\filewriter.txt") ){

    fileWriter.write("data 1");
    fileWriter.write("data 2");
    fileWriter.write("data 3");

}

Notice how there is no longer any explicit close() method call to the FileWriter instance. The try-with-resources construct takes care of that.

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC