Last Updated : 23 Jul, 2025
Java FileWriter class of the java.io package is used to write data in character form to a file. The FileWriter class in Java is used to write character-oriented data to a file. It is a character-oriented class that is used for file handling in Java.
FileWriter extends OutputStreamWriter and Writer classes. It implements the following interfaces:
Constructors of FileWriter Classpublic class FileWriter extends OutputStreamWriter
Constructor
Description
FileWriter(File file)
It constructs a FileWriter object given a File object. It throws an IOException if the file exists but is a directory rather than a regular file does not exist but cannot be created, or cannot be opened for any other reason.
FileWriter(File file, boolean append)
It constructs a FileWriter object given a File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. It throws an IOException if the file exists but is a directory rather than a regular file or does not exist but cannot be created, or cannot be opened for any other reason.
FileWriter(FileDescriptor fd)
It constructs a FileWriter object associated with a file descriptor.
FileWriter(File file, Charset charset)
It constructs the fileWriter when file and charset is given.
FileWriter(File file, Charset charset, boolean append)
It constructs the fileWriter when file and charset is given and a boolean indicating whether to append the data written or not.
FileWriter(String fileName)
It constructs a FileWriter object given a file name.
FileWriter(String fileName, Boolean append)
It constructs a FileWriter object given a file name with a Boolean indicating whether or not to append the data written.
FileWriter(String fileName, Charset charset)
It constructs a FileWriter when a fileName and charset is given.
FileWriter(String fileName, Charset charset, boolean append)
It constructs a fileWriter when a fileName and a charset are given and a boolean variable indicating whether to append data or not.
Example 1: Writing Characters to a File using FileWriter class
Java
// Writing Characters in File using FileWriter
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
class WriteFile
{
public static void main (String[] args)
{
// Scanner Class Instance
Scanner scn=new Scanner(System.in);
// User input for the File Path
String path=scn.nextLine();
// Writing in File
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.");
}
// Exception Thrown
catch (IOException e) {
System.out.println("An error occurred while writing"
+ " to the file: " + e.getMessage());
}
}
}
Output:
Example 2: Appending Data to a File
Java
// Appending Data to a File
import java.io.*;
class AppendingFile
{
public static void main (String[] args)
{
// 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:
Methods of FileWriter ClassMethod
Descriptionvoid write(int a)
Writes a single character specified by an Integer value.
void write(String text) Write the string into FileWriter. void write(char c) Write the char into FileWriter. void write(char[] c) Write a char array into FileWriter.void write(String str, int pos, int length)
Writes a portion of the string from position pos until the length number of characters.
void write(char ch[], int pos, int length)
Writes the position of characters from array ch[] from position pos till length number of characters.
void flush() Flushes the data of FileWriter. void close() Close the FileWriter.int getEncoding()
Returns the character encoding used by FileWriter.
Methods of Writer ClassMethod
Description append(char c) Appends the specified character to this writer. append(CharSequence csq) Appends the specified character sequence to this writer. append(CharSequence csq, int start, int end) Appends a subsequence of the specified character sequence to this writer. close() Closes the stream, flushing it first. nullWriter() Returns a new Writer which discards all characters. write(char[] cbuf) Writes an array of characters. write(String str) Writes a string. Overwriting vs Appending the FileOverwriting a File: While creating a Java FileWriter, we can decide whether we want to append the file to an existing file, or we want to overwrite any existing file. This can be decided by choosing the appropriate constructor. The constructor for overwriting any existing file takes only one parameter that is a file name.
Writer fileWriter = new FileWriter("C:\\FileHandling\\FileDescriptor\\FILE.txt");
Appending to a File: The constructor for appending the file or overwriting the file, takes two parameters, the file name and a boolean variable which decides whether to append or overwrite the file
FileWriter vs FileOutputStreamWriter fileWriter = new FileWriter("C:\\FileHandling\\FileDescriptor\\FILE.txt", true); // appends to file
Writer fileWriter = new FileWriter(C:\\FileHandling\\FileDescriptor\\FILE.txt", false); // overwrites file
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4