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
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:
OutputFileWriter Class Hierarchy
FileWriter Class HierarchyConstructors 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
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
FileWriter vs FileOutputStream
| Basis | FileWriter | FileOutputStream |
|---|
| Stream Type | Character-oriented | Byte-oriented |
| Used For | Writing text data | Writing binary data |
| Data Format | Writes characters (char, String) | Writes raw bytes (byte[]) |
| Encoding Support | Uses platform default encoding | Does not handle character encoding |
| Best Suited For | Text files (.txt, .log) | Binary files (.jpg, .mp3, .pdf) |
Related Articles
Explore
Java Basics
OOP & Interfaces
Collections
Exception Handling
Java Advanced
Practice Java