Java IO – Read and write text files

Reading Character Streams – Classes

All classes that read character stream extend The important classes are:

  • InputStreamReader-This class reads bytes from an underlying stream and converts it to characters using a Charset. It acts as a bridge between a byte stream and character stream
  • PipedReader- This class connects to a PipedWriter and reads characters from it.
  • BufferedReader-Reads characters from a character input stream and buffers data. The buffering can efficiently read lines or array of characters. It is wrapped around another reader that reads a single character of data such as FileReader or InputStreamReader
  • FileReader-This is a convenience class for reading characters from a file. It extends InputStreamReader. It uses the default encoding scheme and byte buffer. To specify another encoding scheme or byte buffer create an implementation of InputSteamReader on a FileInputStream

Writing Character Streams – Classes

All classes that write character stream extend The important classe are:

  • OutputStreamWriter-This class encodes characters to bytes using a specified charset.It acts as a bridge between a character stream and byte stream
  • FileWriter-This class extends OutputStreamWriter and is a convenience method for writing to files. It uses a default encoding and buffer size. To specify a different buffer size or encoding scheme, create an OutputStreamWriter on a FileOutputStream
  • PipedWriter-This can be connected to a PipedReader. It writes characters to the stream which is read by the PipedReader.
  • PrintWriter-This class formats objects and writes them to a character stream. It has automatic flushing for some methods. It never throws IOException. This class is very useful, for example, to write to a file the method println(String s) writes the string s, writes a newLine and then flushes the stream if auto flushing is enabled
  • BufferedWriter-Writes characters to a character stream after buffering them.A newLine() method is provided that writes the platforms line separator to the stream.

Read and write text files without buffering

classic example for reading and writing to a text file

FileReader reader = null;
FileWriter writer = null;
try {
	reader = new FileReader("in.txt");
	writer = new FileWriter("out.txt");
	int a = 0;
		while ((a = != -1) {
	} catch (FileNotFoundException e) {
} catch (IOException e) {
} finally {
	try {
	} catch (IOException e) {

Read and write text files with buffering

Reading and writing using buffered reader and writer. Note the use of the apache IOUtils class to close the stream.

BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
	bufferedReader = new BufferedReader(new FileReader("in.txt"));
	bufferedWriter = new BufferedWriter(new FileWriter("out.txt"));
	String s = "";
	while ((s = bufferedReader.readLine()) != null) {
		// write a new line
		// flush
} catch (FileNotFoundException e) {
} catch (IOException e) {
} finally {
	// close without throwing exception

Read Unicode file

The following example demonstrates how to read a file encoded in unicode

Reading and Writing – Convenient methods

Get the contents of an input stream as byte array

Get the contents of inputstream as list of Strings

Get the contents of inputstream as list of Strings

Convert a string to input stream

Write array of bytes to output stream.

Write a collection of lines to outputstream

read the contents of a file into a string. There is a similar write method too.

Read file into a list of strings

use to get a buffered reader, writer and inputstream

1 thought on “Java IO – Read and write text files”

  1. A stream transports data from some point A to some other point B. It may modify the data on its way. A stream is not active on its own. Some other object must push data into a stream or pull data from a stream. Many thanks for sharing this.


Leave a Comment