# How to Read and Write Text Files in Java

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Dec 4, 2019, updated Dec 5, 2019. Topics: Java.
Canonical URL: https://attacomsian.com/blog/java-read-write-text-files

> A quick tutorial to learn how to read and write text file in Java.

In my [previous article](https://attacomsian.com/blog/java-read-write-files), 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:

```java
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:

```java
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](https://attacomsian.com/blog/how-to-read-a-file-line-by-line-in-java) as shown below:

```java
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:

```java
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:

```java
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:

```java
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](https://attacomsian.com/blog/java-read-write-files) tutorial.
