mydomain
No ADS
No ADS

Java FileWriter Tutorial with Examples

  1. FileWriter
  2. Examples

1. FileWriter

No ADS
FileWriter is a subclass of OutputStreamWriter, used to write text files.
FileWriter has no other methods but the ones inherited from OutputStreamWriter. You can actually use OutputStreamWriter to write characters to any target. However, FileWriter is specifically designed to write characters to the system file.
FileWriter​ constructors
FileWriter​(File file)     
 
FileWriter​(FileDescriptor fd)     

FileWriter​(File file, boolean append)     

FileWriter​(File file, Charset charset)     

FileWriter​(File file, Charset charset, boolean append)     
 
FileWriter​(String fileName)     

FileWriter​(String fileName, boolean append)     

FileWriter​(String fileName, Charset charset)     

FileWriter​(String fileName, Charset charset, boolean append)
Note: Constructors with the Charset parameter have been added to FileWriter since Java 11. Therefore, if you are using previous Java version and want to write a file with specified encoding, use the OutputStreamWriter class for replacement.

2. Examples

No ADS
FileWriterEx1.java
package org.o7planning.filewriter.ex;

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

public class FileWriterEx1 {

    public static void main(String[] args) throws IOException {
        // Windows: C:/SomeFolder/out-file.txt
        File outFile = new File("/Volumes/Data/test/out-file.txt");
        
        outFile.getParentFile().mkdirs();
        
        FileWriter fileWriter = new FileWriter(outFile);
        
        System.out.println("Writer file: " + outFile.getAbsolutePath());
        System.out.println("With encoding: " + fileWriter.getEncoding());

        fileWriter.write("Line 1");
        fileWriter.write("\n");
        fileWriter.write("Line 2");
        fileWriter.write("\n");
        fileWriter.write("Line 3");

        fileWriter.close();
    }
}
Output:
out-file.txt
Line 1
Line 2
Line 3
FileWriter also allows you to append data to an existing file, if the file does not exist it will be created.
FileWriterEx2.java
package org.o7planning.filewriter.ex;

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

public class FileWriterEx2 {

    public static void main(String[] args) throws IOException {
        // Windows: C:/SomeFolder/out-file.txt
        File outFile = new File("/Volumes/Data/test/out-file.txt");
        
        outFile.getParentFile().mkdirs();
        
        // FileWriter(File outFile, boolean append)
        FileWriter fileWriter = new FileWriter(outFile, true);
        
        System.out.println("Writer file: " + outFile.getAbsolutePath());
        System.out.println("With encoding: " + fileWriter.getEncoding());

        fileWriter.write("Line 4");
        fileWriter.write("\n");
        fileWriter.append("Line 5").append("\n");
        fileWriter.close();
    }
}
For example, use FileWriter to write a file with UTF-16 encoding, then read the file with FileInputStream to view the bytes on the file.
FileWriter_UTF16_Ex1.java
package org.o7planning.filewriter.ex;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class FileWriter_UTF16_Ex1 {
    // Windows: C:/SomeFolder/out-utf16-file.txt
    private static final String filePath = "/Volumes/Data/test/out-utf16-file.txt";

    public static void main(String[] args) throws IOException {
        System.out.println("--- Write UTF-16 File ---");
        write_UTF16_File();
        
        System.out.println("--- Read File as Binary Stream ---");
        readAsInputStream();
    }

    private static void write_UTF16_File() throws IOException {
        File outFile = new File(filePath);
        outFile.getParentFile().mkdirs();

        // FileWriter(File outFile, boolean append)
        FileWriter fileWriter = new FileWriter(outFile, StandardCharsets.UTF_16);
        fileWriter.write("JP日本-八洲");
        fileWriter.close();
    }

    private static void readAsInputStream() throws IOException {
        File file = new File(filePath);

        FileInputStream fis = new FileInputStream(file);

        int byteCode;
        while ((byteCode = fis.read()) != -1) {
            System.out.println((char) byteCode + " " + byteCode);
        }
        fis.close();
    }
}
Output:
--- Write UTF-16 File ---
--- Read File as Binary Stream ---
þ 254
ÿ 255
0
J 74
0
P 80
e 101
å 229
g 103
, 44
0
- 45
Q 81
k 107
m 109
2 50
Below is an illustration of the bytes on the FileWriter and the bytes in the file just written by FileWriter:
The first two bytes (254,255) in the UTF-16 file are used to mark that it starts an UTF-16 encoded data.
Check out my articles about InputStreamReader and OutputStreamWriter to learn more details about how Java reads and writes UTF-16 and UTF-8 encodings.
No ADS

Java IO Tutorials

Show More
No ADS