Java File Writer Example

0

Classes java.io.FileInputStream and java.io.FileOutputStream are concrete subclasses of java.io.InputStream and java.io.OutputStream, their main goal is to write bytes to a file stream and real bytes from a file stream. We also have another class of readers and writers which extend java.io.Reader and java.io.Writer, these classes are character based so they read characters from stream and write characters to stream.
In this article, we will see some examples of writing text to a file using FileWriter. If you want to write raw bytes, consider using a FileOutputStream.

FileWriter Example

First create FileWriter object. You need to pass the file name of the file to be written to the constructor of FileWriter. If the file cannot be created, or opened, then an IOException will be thrown. To write a string call write(). Make sure the FileWriter is closed in the finally block.

FileWriterExample:

package com.javarticles.io;

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

public class FileWriterExample {
    public static void main(String[] args) throws IOException {
        FileWriter f = new FileWriter("test.txt");
        try {
            f.write("line1");
            f.write("line2");
        } finally {
            f.close();
        }
    }
}

Output:

line1line2

PrintWriter Example

We can wrap the above FileWriter object using a PrintWriter. We use PrintWriter to print the formatted representations of objects to a text-output stream.
If print null, ‘null’ string will be printed.

PrintWriterExample:

package com.javarticles.io;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class PrintWriterExample {
    public static void main(String[] args) throws IOException {
        FileWriter f = new FileWriter("test1.txt");
        PrintWriter printWriter = new PrintWriter(f);
        try {
            printWriter.println("line1");
            printWriter.println((String) null);
            printWriter.println("line2");            
        } finally {
            printWriter.close();
        }
    }
}

Output:

line1
null
line2

BufferWriter Example

If you want to write text to a character-output stream, buffering characters to provide for the efficient writing, you need to wrap FileWriter in a BufferedWriter object.

BufferedWriterExample:

package com.javarticles.io;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class BufferedWriterExample {
    public static void main(String[] args) throws IOException {
        FileWriter f = new FileWriter("test2.txt");        
        BufferedWriter bf = new BufferedWriter(f);
        PrintWriter printWriter = new PrintWriter(bf);
        try {
            printWriter.println("line1");
            printWriter.println((String) null);
            printWriter.println("line2");            
        } finally {
            printWriter.close();
        }
    }
}

Output:

line1
null
line2

Download the source code

This was an example about writing text to files.

You can download the source code here: javaFileWriterExample.zip

About Author

Ram’s expertise lies in test driven development and re-factoring. He is passionate about open source technologies and loves blogging on various java and open-source technologies like spring.
You can reach him at [email protected]

Comments are closed.