English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Java Flow Control

Java Array

Java Object-Oriented (I)

Java Object-Oriented (II)

Java Object-Oriented (III)

Java Exception Handling

Java List

Java Queue (Queue)

Java Map Collection

Java Set Collection

Java Input Output (I/)

Java Reader/Writer

Java Other Topics

Java FileWriter Class

In this tutorial, we will learn about Java FileWriter and its methods with the help of examples.

The java.io package's FileWriter class can be used to write data (in character units) to a file.

It inherits the OutputStreamWriter class.

Before further understanding FileWriter, make sure you understandJava File.

Create a FileWriter

To create a file writer, we must first import the Java.io.FileWriter package. After importing the package, we can create a file writer.

1.Using the filename

FileWriter output = new FileWriter(String name);

Here, we create a file writer that will link to the specified filename.

2.Using the file object

FileWriter input = new FileWriter(File fileObj);

Here, we create a file writer that will link to the file specified by the file object.

In the above example, the data is stored using some default character encoding.

However, due to Java 11we can also specify the type of character encoding (UTF8orUTF16)

FileWriter input = new FileWriter(String file, Charset cs);

Here, we use the Charset class to specify the character encoding of the file writer.

FileWriter method

The FileWriter class provides implementations for different methods that appear in the Writer class.

write() method

  • write() -Write a character to the writer

  • write(char[] array) -Write the characters in the specified array to the writer

  • write(String data) -Write the specified string to the writer

Example: FileWriter writes data to a file

import java.io.FileWriter;
public class Main {
  public static void main(String args[]) {
    String data = "This is the data in the output file";
    try {
      // Create FileWriter
      FileWriter output = new FileWriter("output.txt");
      // Write a string to the file
      output.write(data);
      //Close writer
      output.close();
    }
    catch (Exception e) {
      e.getStackTrace();
    }
  }
}

In the above example, we created a file writer named output. The output reader is associated withoutput.txtFile link.

FileWriter output = new FileWriter("output.txt");

To write data to the file, we used the write() method.

Here, when we run the program,output.txtThe file will be filled with the following content.

This is a line of text inside the file.

getEncoding() method

The getEncoding() method can be used to obtain the encoding type used for writing data. For example,

import java.io.FileWriter;
import java.nio.charset.Charset;
class Main {
  public static void main(String[] args) {
    String file = "output.txt";
    try {
      //Create a FileReader using default encoding
      FileWriter output1 = new FileWriter(file);
      //Create a FileReader with specified encoding
      FileWriter output2 = new FileWriter(file, Charset.forName("UTF8");
      //Returns the character encoding of the reader
      System.out.println("output1The character encoding: " + output1.getEncoding());
      System.out.println("output2The character encoding: " + output2.getEncoding());
      // Close reader
      output1.close();
      output2.close();
    }
    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

Output Result

output1character encoding: Cp1252
output2character encoding: UTF8

In the above example, we created2file writers, named output1and output2.

  • output1No character encoding is specified. Therefore, the getEncoding() method returns the default character encoding.

  • output2Specify Character EncodingUTF8. Therefore, the getEncoding() method returns the specified character encoding.

Note: We have used the Charset.forName() method to specify the type of character encoding.

close() Method

To close the file writer, we can use the close() method. Once the close() method is called, the writer cannot be used to write data.

Other Methods of FileWriter

MethodDescription
flush()Force all existing data in the writer to be written to the corresponding destination
append()Insert the specified character into the current writer