FileWriter Tutorial

The purpose of the FileWriter class is to simply write character based files. The FileWriter class implements AutoClosable so we can use the try-with-resources type exception handling. There are five overloaded constructors in the FileWriter class, this tutorial will focus only the two that take a String parameter representing the file name. Regarding the other three constructors, I will revisit this class later once I have demonstrated the File and FileDescriptor classes. It also interesting to note that the FileWriter class does not define or override any public methods, so it inherits all of its methods from its superclass OutputStreamWriter; OutputStreamWriter is a subclass of Writer which is a subclass of Object and that describes the class hierarchy.

FileWriter(String filename)
FileWriter(String filename, boolean append)



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md FW
C:\Java>cd FW
C:\Java\FW>notepad FW.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


import java.io.*;
class FW {
    public static void main(String args[]) {
        try (FileWriter fw = new FileWriter("Example.txt")) {
            fw.write("First line of the text file.");
            fw.write(13);
            fw.write('\n');
            fw.write("Second line of the text file.");
            fw.write(13);
            fw.write('\n');
            fw.write("Third line of the text file.");
            fw.write(13);
            fw.write('\n');
            fw.write("Fourth line of the text file.");
            fw.write(13);
            fw.write('\n');
            fw.write("Fifth line of the text file.");
        } catch(IOException e) {
            System.out.println(e.getMessage());
        }
/*        //open the file in notepad and check it out. Let's write it again, it will get overwritten
        try (FileWriter fw = new FileWriter("Example.txt")) {
            fw.write("First line of the text file.");
            fw.write(13);
            fw.write('\n');
            fw.write("Second line of the text file.");
            fw.write(13);
            fw.write('\n');
            fw.write("Third line of the text file.");
            fw.write(13);
            fw.write('\n');
            fw.write("Fourth line of the text file.");
            fw.write(13);
            fw.write('\n');
            fw.write("Fifth line of the text file.");
            fw.write(13);
            fw.write('\n');
        } catch(IOException e) {
            System.out.println(e.getMessage());
        }
        //Let's use the constructor that will allow us to append to the file.
        try (FileWriter fw = new FileWriter("Example.txt", true)) {
            fw.write("First line of the text file.");
            fw.write(13);
            fw.write('\n');
            fw.write("Second line of the text file.");
            fw.write(13);
            fw.write('\n');
            fw.write("Third line of the text file.");
            fw.write(13);
            fw.write('\n');
            fw.write("Fourth line of the text file.");
            fw.write(13);
            fw.write('\n');
            fw.write("Fifth line of the text file.");
        } catch(IOException e) {
            System.out.println(e.getMessage());
        }

        // create a file in another folder
        try (FileWriter fw = new FileWriter("c:\\Java\\Sample.txt", true)) {
            fw.write("Hello FileWriter!");
            fw.write(13);
            fw.write('\n');
            fw.write("So easy to make files :)");
        } catch(IOException e) {
            System.out.println(e.getMessage());
        }
*/
    }
}

Now switch back to the command prompt (CMD) and type in javac FW.java and press Enter.
Now type in java FW and press Enter.


C:\Java\FW>javac FW.java
C:\Java\FW>java FW
First line of the text file.
Second line of the text file.
Third line of the text file.
Fourth line of the text file.
Fifth line of the text file.


Final thoughts

Stay tuned to my next tutorial where I will wrap FileWriter into a BufferedWriter and we will gain more functionality.


Tutorials