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
FileWriter Class HierarchyFileWriter 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
| Constructor | Description |
|---|
| 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:
OutputExample 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:
OutputMethods of FileWriter Class
| Method | Description |
|---|
| 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
| Method | Description |
|---|
| 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
| Mode | Constructor Example | Behavior |
|---|
| Overwrite | new FileWriter("file.txt") | Replaces existing content. |
|---|
| Append | new FileWriter("file.txt", true) | Adds data at the end without deleting old content. |
|---|
FileWriter vs FileOutputStream
| Feature | FileWriter | FileOutputStream |
|---|
| Data type | Characters (16-bit, Unicode) | Bytes (8-bit) |
|---|
| Usage | Text files | Binary data (images, audio, etc.) |
|---|
| Strings | Directly supported | Needs OutputStreamWriter |
|---|
Related Articles
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java