In my previous article, I wrote about reading and writing different types of files in Java. In this quick article, you'll learn how to read and write text files in Java.

Reading Text Files

Java provides multiple APIs to read a text file. The following example demonstrates how you can use the FileReader class to read every single character from the input file and print them on the console:

try {
    // create a reader
    FileReader reader = new FileReader("input.txt");

    // read every characer
    int ch;
    while ((ch = reader.read()) != -1) {
        System.out.print((char) ch);
    }

    // close the reader
    reader.close();

} catch (IOException ex) {
    ex.printStackTrace();
}

If your text file has encoding other than the default character encoding of the operating system, use the InputStreamReader class instead:

try {
    // create a reader
    FileInputStream fis = new FileInputStream("input.txt");
    InputStreamReader reader = new InputStreamReader(fis, Charset.forName("UTF_16"));

    // read every characer
    int ch;
    while ((ch = reader.read()) != -1) {
        System.out.print((char) ch);
    }

    // close the reader
    reader.close();

} catch (IOException ex) {
    ex.printStackTrace();
}

Finally, we have the BufferedReader class that can be used to read a text file line by line as shown below:

try {
    // create a reader
    BufferedReader br = new BufferedReader(new FileReader("input.txt"));

    // read until end of file
    String line;
    while ((line = br.readLine()) != null) {
        System.out.println(line);
    }

    // close the reader
    br.close();

} catch (IOException ex) {
    ex.printStackTrace();
}

Writing Text Files

The following example shows how you can use the FileWriter class to write data to a text file:

try {
    // create a writer
    FileWriter writer = new FileWriter("output.txt");

    // write data to file
    writer.write("Hey, there!");
    writer.write("\n");
    writer.write("What's up?");

    // close the writer
    writer.close();

} catch (IOException ex) {
    ex.printStackTrace();
}

The above example uses the default character encoding of the operating system to write text to a file. If you want to specify a different character encoding, use the OutputStreamWriter class instead:

try {
    // create a writer
    FileOutputStream fos = new FileOutputStream("output.txt");
    OutputStreamWriter writer = new OutputStreamWriter(fos, Charset.forName("UTF_16"));

    // write data to file
    writer.write("Hey, there!");
    writer.write("\n");
    writer.write("What's up?");

    // close the writer
    writer.close();

} catch (IOException ex) {
    ex.printStackTrace();
}

Finally, we have the BufferedWriter class for writing a text file as shown below:

try {
    // create a writer
    BufferedWriter bw = new BufferedWriter(new FileWriter("output.txt"));

    // write text to file
    bw.write("Hey, there!");
    bw.newLine();
    bw.write("Do you need a coffee?");

    // close the writer
    bw.close();

} catch (IOException ex) {
    ex.printStackTrace();
}

For more file read and write examples, check out the Reading and Writing Files in Java tutorial.

✌️ Like this article? Follow @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. Buy me a coffee (cost $3)

Need help to start a new Spring Boot or MEAN stack project? I am available for contract work. Hire me to accomplish your business goals with engineering and design. Let’s talk about your project: hi@attacomsian.com.