Reading Writing Text  «Prev  Next»


Lesson 8InputStreamReader
Objective Write a program that reads a text file in the default character encoding and prints it on System.out

Java InputStreamReader

The InputStreamReader Class

The most basic concrete subclass of Reader is InputStreamReader:
public class InputStreamReader extends Reader

Example 4.8 uses an InputStreamReader to read a file in a user-specified encoding. The FileConverter reads the name of the input file, the name of the of the output file, the input encoding, and the output encoding. Characters that are not available in the output character set are replaced by the substitution character, generally the question mark.
The first constructor uses the platform's default encoding, as given by the system property file.encoding. The second one uses the specified encoding. For example, to attach an InputStreamReader to System.in with the default encoding (generally ISO Latin-1):
Example 4.8 CharacterSetConverter
import java.io.*;

public class CharacterSetConverter {
 public static void main(String[] args) {
  if (args.length < 2) {
   System.err.println("Usage: java CharacterSetConverter " + 
   "infile_encoding outfile_encoding infile outfile");
	return;
  }
  try {
   File infile = new File(args[2]);
   File outfile = new File(args[3]);
   if (infile.getCanonicalPath().equals(outfile.getCanonicalPath())) {
    System.err.println("Can't convert file in place");
	return;
   }
   FileInputStream fin = new FileInputStream(infile);
   FileOutputStream fout = new FileOutputStream(outfile);
   InputStreamReader isr = new InputStreamReader(fin, args[0]);
   OutputStreamWriter osw = new OutputStreamWriter(fout, args[1]);
	while (true) {
     int c = isr.read();
     if (c == -1)
      break; // end of stream
      osw.write(c);
     }
   osw.close();
    isr.close();
   }  
  catch (IOException e) {
    System.err.println(e);
  }
 }
}

Since this is just a simple example, the user interface is trivial. A more realistic command-line interface would provide a set of flags and sensible defaults. Even better would be a graphical user interface. I'll demonstrate that at the end of the chapter, when we return to the file viewer program.

Default Character Encoding

Readers do not generally themselves produce data. Instead they tend to act like filters, sitting between a source of data like a file or an input stream and the rest of the program.
The reader reads bytes from the source and converts them into characters for the program.
The InputStreamReader class is a basic reader that can be chained to any input stream and used to read data using the methods discussed in the previous lesson. Aside from implementing the abstract methods of java.io.Reader, such as read(), skip(), close(), and so on, the InputStreamReader class has two constructors.
To read from the file chinese.txt, which is known to be written in the Chinese Big 5 encoding, you might write the following code:

try {
  FileInputStream fis = new FileInputStream("chinese.txt");
  InputStreamReader isr = new InputStreamReader(fis, "Big5");
  // read the file...
}
catch (IOException e) {
  System.err.println(e);
}

Reading From Input Streams - Exercise

Click the Exercise link below to write a program that reads a text file in the default character encoding and prints it on System.out.
Reading From Input Streams - Exercise