Java - FileWriter Class



Introduction

The Java FileWriter class is a convenience class for writing character files. Following are the important points about FileWriter −

  • The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable.

  • FileWriter is meant for writing streams of characters. For writing streams of raw bytes, use FileOutputStream.

Class declaration

Following is the declaration for Java.io.FileWriter class −

public class FileWriter
   extends OutputStreamWriter

Field

Following are the fields for Java.io.FileWriter class −

  • protected Object lock − This is the object used to synchronize operations on this stream.

Class constructors

Sr.No. Constructor & Description
1

FileWriter(File file)

This constructor creates a FileWriter object given a File object.

2

FileWriter(File file, boolean append)

This constructor creates a FileWriter object given a File object with a boolean indicating whether or not to append the data written.

3

FileWriter(FileDescriptor fd)

This constructor creates a FileWriter object associated with the given file descriptor.

4

FileWriter(String fileName)

This constructor creates a FileWriter object, given a file name.

5

FileWriter(String fileName, boolean append)

This constructor creates a FileWriter object given a file name with a boolean indicating whether or not to append the data written.

Once you have FileWriter object in hand, then there is a list of helper methods, which can be used to manipulate the files.

Sr.No. Method & Description
1 void write(int c)

This method writes a single character.

2 void write(char [] c, int offset, int len)

This method writes a portion of an array of characters starting from offset and with a length of len.

3 void write(String s, int offset, int len)

This method writes a portion of a String starting from offset and with a length of len.

Example - Usage of FileWriter write() method

The following example shows the usage of Java FileWriter write() method. We've created a File reference with name Hello1.txt to be created in current directory. Then we're creating a new file using createNewFile(). Now a FileWriter object is created by passing the file reference created earlier and some content is written to the file. Using FileReader() class, we're reading that file and its contents are printed.

FileWriterDemo.java

package com.tutorialspoint;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterDemo {

   public static void main(String args[])throws IOException {
      File file = new File("Hello1.txt");
      
      // creates the file
      file.createNewFile();
      
      // creates a FileWriter Object
      FileWriter writer = new FileWriter(file); 
      
      // Writes the content to the file
      writer.write("This\n is\n an\n example\n"); 
      writer.flush();
      writer.close();

      // Creates a FileReader Object
      FileReader fr = new FileReader(file); 
      char [] a = new char[50];
      fr.read(a);   // reads the content to the array
      
      for(char c : a)
         System.out.print(c);   // prints the characters one by one
      fr.close();
   }
}

Output

This
is
an
example

Example - Usage of FileWriter Class

The following example shows the usage of Java FileWriter class. We've created a File reference with name Hello1.txt to be created in a provided directory. Then we're creating a new file using createNewFile(). Now a FileWriter object is created by passing the file reference created earlier and some content is written to the file. Using FileReader() class, we're reading that file and its contents are printed.

FileWriterDemo.java

package com.tutorialspoint;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterDemo {

   public static void main(String args[])throws IOException {
      File file = new File("Hello1.txt");
      
      // creates the file
      file.createNewFile();
      
      // creates a FileWriter Object
      FileWriter writer = new FileWriter(file); 
      
      // Writes the content to the file
      writer.write("This\n is\n an\n example\n"); 
      writer.flush();
      writer.close();

      // Creates a FileReader Object
      FileReader fr = new FileReader(file); 
      char [] a = new char[50];
      fr.read(a);   // reads the content to the array
      
      for(char c : a)
         System.out.print(c);   // prints the characters one by one
      fr.close();
   }
}

Output

This
is
an
example

Example - Writing to a File Using FileWriter(FileDescriptor fd)

The following example shows the usage of Java FileWriter(FileDescriptor fd) constructor.

FileWriterDemo.java

package com.tutorialspoint;

import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

public class FileWriterDemo {
   public static void main(String[] args) {
      try {
         // Step 1: Open FileOutputStream to get the FileDescriptor
         FileOutputStream fos = new FileOutputStream("example.txt");

         // Step 2: Get the FileDescriptor from FileOutputStream
         FileDescriptor fd = fos.getFD();

         // Step 3: Create FileWriter using the FileDescriptor
         FileWriter fw = new FileWriter(fd);

         // Step 4: Write data using FileWriter
         fw.write("Hello, FileWriter with FileDescriptor!");

         // Step 5: Flush and Close streams
         fw.flush();
         fos.close();

         System.out.println("Data written successfully using FileWriter with FileDescriptor.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Data written successfully using FileWriter with FileDescriptor.

The file "example.txt" now contains−

Hello, FileWriter with FileDescriptor!

Explanation

  • Create a FileOutputStream for "example.txt" to obtain a FileDescriptor.

  • Retrieve the FileDescriptor using fos.getFD().

  • Pass the FileDescriptor to FileWriter, so it writes to the same open file.

  • Write data using fw.write("Hello, FileWriter with FileDescriptor!").

  • Flush and close the streams to ensure data is properly saved.

Advertisements