VOOZH about

URL: https://www.geeksforgeeks.org/java/filewriter-class-in-java/

⇱ Java FileWriter Class - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Java FileWriter Class

Last Updated : 4 Nov, 2025

The FileWriter class in Java is used to write character data to files. It extends OutputStreamWriter and handles characters directly, making it ideal for writing text files with either the default or a specified encoding.

  • Character-Oriented Stream: Designed for writing text (characters).
  • Convenient for Text Files: Works with char, String and char[].
  • Append Option Available: Can open a file in append mode to add content.

Class Declaration

public class FileWriter extends OutputStreamWriter

FileWriter extends OutputStreamWriter, which implements the Writer abstract class.

Example 1: Writing Characters to a File using FileWriter class

Output:

👁 WriteFile
Output

FileWriter Class Hierarchy

👁 extends
FileWriter Class Hierarchy

Constructors of FileWriter Class

1. FileWriter(String fileName): Creates a FileWriter for the given file name.

FileWriter fw = new FileWriter("example.txt");

2. FileWriter(String fileName, boolean append): Creates a FileWriter for a given File. If append is true, data is added to the end instead of overwriting.

FileWriter fw = new FileWriter("example.txt", true); // append mode

3. FileWriter(File file): Creates a FileWriter for a given File. Throws IOException if the file is a directory, cannot be created, or opened.

File file = new File("example.txt");
FileWriter fw = new FileWriter(file);

4. FileWriter(File file, boolean append): Creates a FileWriter for a given File. If append is true, data is added to the end instead of overwriting.

FileWriter fw = new FileWriter(file, true);

5. FileWriter(FileDescriptor fdObj): Creates a FileWriter linked to the given file descriptor.

FileWriter fw = new FileWriter(FileDescriptor.out);

Appending Data to a File

Output:

👁 AppendingFile
Output

Methods of FileWriter Class

MethodDescription
write(int c)Writes a single character.
write(char[] cbuf)Writes an array of characters.
write(char[] cbuf, int off, int len)Writes a portion of a character array.
write(String str)Writes a string.
write(String str, int off, int len)Writes a portion of a string.
append(char c)Appends the specified character to the writer.
append(CharSequence csq)Appends the specified character sequence.
append(CharSequence csq, int start, int end)Appends a subsequence of the specified character sequence.
flush()Flushes the writer (forces any buffered characters to be written).
close()Closes the writer, flushing it first.

FileWriter vs FileOutputStream

BasisFileWriterFileOutputStream
Stream TypeCharacter-orientedByte-oriented
Used ForWriting text dataWriting binary data
Data FormatWrites characters (char, String)Writes raw bytes (byte[])
Encoding SupportUses platform default encodingDoes not handle character encoding
Best Suited ForText files (.txt, .log)Binary files (.jpg, .mp3, .pdf)

Related Articles

Comment
Article Tags: