The Wayback Machine - https://web.archive.org/web/20250916113214/https://www.geeksforgeeks.org/java/filewriter-class-in-java/
Open In App

Java FileWriter Class

Last Updated : 29 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Java FileWriter class is used to write text data to files. It writes characters directly to a file, making it useful for creating and modifying text files. By default, it overwrites the file content, but it can also be used in append mode to add data without deleting existing content. For better performance, it’s often wrapped with BufferedWriter.

FileWriter Class Hierarchy

extends
FileWriter Class Hierarchy

FileWriter extends OutputStreamWriter and Writer classes. It implements the following interfaces:

  • Closeable
  • Flushable
  • Appendable
  • AutoCloseable

Java FileWriter Class Declaration

public class FileWriter extends OutputStreamWriter 

Constructors of FileWriter Class

ConstructorDescription
FileWriter(File file)Creates a FileWriter for a given File. Throws IOException if the file is a directory, cannot be created, or opened.
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(FileDescriptor fd)Creates a FileWriter linked to the given file descriptor.
FileWriter(File file, Charset charset)Creates a FileWriter for the given File with the specified charset.
FileWriter(File file, Charset charset, boolean append)Same as above but allows appending data if append is true.
FileWriter(String fileName)Creates a FileWriter for the given file name.
FileWriter(String fileName, boolean append)Creates a FileWriter for the given file name with an option to append.
FileWriter(String fileName, Charset charset)Creates a FileWriter for the given file name with the specified charset.
FileWriter(String fileName, Charset charset, boolean append)Creates a FileWriter for the given file name with charset and an option to append.

Example 1: Writing Characters to a File using FileWriter class

Java
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

class WriteFile
{
    public static void main (String[] args)
    {
        Scanner scn=new Scanner(System.in);
      	
      	String path=scn.nextLine();

        try (FileWriter writer = new FileWriter(path)) 
        {
            String str= "Hello Geeks!\nThis is about Programming";
            writer.write(str);

            System.out.println("Data written to the file successfully.");
        
        } 
     
      	catch (IOException e) {
            System.out.println("An error occurred while writing" + " to the file: " + e.getMessage());
        }
      
    }
}

Output:

WriteFile
Output

Example 2: Appending Data to a File

Java
import java.io.*;

class AppendingFile 
{
    public static void main (String[] args) 
    {
        String fileName = "output.txt";
      	// Appending Data in the File
       	try (FileWriter writer = new FileWriter(fileName, true)) { 
          	
          	// true for append mode
          	writer.write("\nAppending this line to the file.");
          	System.out.println("Data appended to the file successfully.");
		} 
      	catch (IOException e) {
    		System.out.println("An error occurred while appending"
                               + " to the file: " + e.getMessage());
		}
    }
}

Output:

AppendingFile
Output

Methods of FileWriter Class

MethodDescription
void write(int c)Writes a single character (given as an integer value).
void write(String text)Writes an entire string to the file.
void write(char[] c)Writes a character array to the file.
void write(String str, int offset, int length)Writes a portion of a string, starting from offset for length characters.
void write(char[] cbuf, int offset, int length)Writes a portion of a character array, starting from offset for length characters.
void flush()Flushes the data to ensure everything is written to the file.
void close()Closes the FileWriter and releases resources.

Methods of Writer 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.
nullWriter()Returns a writer that discards all characters.

Overwriting vs Appending the File

ModeConstructor ExampleBehavior
Overwritenew FileWriter("file.txt")Replaces existing content.
Appendnew FileWriter("file.txt", true)Adds data at the end without deleting old content.

FileWriter vs FileOutputStream

FeatureFileWriterFileOutputStream
Data typeCharacters (16-bit, Unicode)Bytes (8-bit)
UsageText filesBinary data (images, audio, etc.)
StringsDirectly supportedNeeds OutputStreamWriter

Explore