Java FileWriter Example

The FileWriter class of java.io package makes writing to files in Java as easy as A-B-C. The FileWriter class writes to files as a stream of characters instead of bytes(as in FileOutputStream). Like its input-oriented counterpart FileReader, the FileWriter class is intended for writing “text” based files one character at a time.

Let us look into how FileWriter works in Java.

Constructors

The FileWriter class in Java has five parameterized constructors to allow programmers to manipulate or write to files. Based on the mode of writing, files can be overwritten or appended. Let us take a look at the constructors defined in the FileWriter class.

  • FileWriter(File file) – This constructor accepts a java.io.File object as a parameter and opens the file for writing. By default, this overwrites any previous content of the file.
  • FileWriter(FileDescriptor descriptor) – This constructor creates an object of FileWriter which accepts a java.io.FileDescriptor object as a parameter and opens the file referenced by the descriptor object. By default, it overwrites the previous contents.
  • FileWriter(String filePath) – This constructor accepts a java.lang.String object as a parameter and opens the file referenced by the path or URI contained in the String and overwrites any existing content in the file.
  • FileWriter(File file, boolean append) – This constructor opens accepts a java.io.File and a boolean value as parameter. If the boolean value is true, the existing contents are not overwritten and the new contents are appended
  • FileWriter(String filePath, boolean append) – This constructor opens accepts a java.lang.String and a boolean value as parameter. If the boolean value is true, the existing contents are not overwritten and the new contents are appended.

In both of the last two cases, if the boolean value entered is ‘false’, the contents are overwritten.

If the file is not found or cannot be opened, a FileNotFoundException is thrown.

Methods

The FileWriter class contains methods that allow the programmer to write characters/text to a file. The FileWriter object writes the contents of the file one character at a time.

Let us take a look at the methods we use to write using FileWriter.

  • public void write(int charVal) – This method writes a single character(based on the ASCII value of the input) to the file.
  • public void write(char arr[],int offset,int l) – This method writes the characters from the character array from the offset position, for a length ‘l’ of characters.
  • public void write(String str, int offset, int l) – This method writes the characters from the String object from the offset position for a length ‘l’ of characters.
  • public void flush()Flushes the current FileWriter object.
  • public void close() – Closes the FileWriter object to prevent any resource leaks.

Example

Let us look at an example of writing to a file with FileWriter class.

Code:

Output:

New Write Complete. File Reads:Hello. I’m writing this new.
Append Complete. File Reads
Hello. I’m writing this new. Append works too

Thats all about java FileWriter example.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *