Reading Text Files
When reading characters from a file using the default character encoding, the following two procedures for setting up an InputStreamReader can be used.
Setting up an InputStreamReader which is chained to a FileInputStream (Figure 20.5(a)):
Figure 20.5 Setting Up Readers to Read Characters
Create a FileInputStream:
FileInputStream inputFile = new FileInputStream(“info.txt”);
Create an InputStreamReader which is chained to the FileInputStream:
InputStreamReader reader = new InputStreamReader(inputFile);
The InputStreamReader uses the default character encoding for reading the characters from the file.
Setting up a FileReader which is a subclass of InputStreamReader (Figure 20.5(b)):
Create a FileReader:
FileReader fileReader = new FileReader(“info.txt”);
This is equivalent to having an InputStreamReader chained to a FileInputStream for reading the characters from the file, using the default character encoding.
If a specific character encoding is desired for the reader, the first procedure can be used (Figure 20.5(a)), with the encoding being specified for the InputStreamReader:
Charset utf8 = Charset.forName(“UTF-8”);
FileInputStream inputFile = new FileInputStream(“info.txt”);
InputStreamReader reader = new InputStreamReader(inputFile, utf8);
This reader will use the UTF-8 character encoding to read the characters from the file. Alternatively, we can use one of the FileReader constructors that accept a character encoding:
Charset utf8 = Charset.forName(“UTF-8”);
FileReader reader = new FileReader(“info.txt”, utf8);
A BufferedReader can also be used to improve the efficiency of reading characters from the underlying stream, as explained later in this section (p. 1251).
Using Buffered Writers
A BufferedWriter can be chained to the underlying writer by using one of the following constructors:
BufferedWriter(Writer out)
BufferedWriter(Writer out, int size)
The default buffer size is used, unless the buffer size is explicitly specified.
Characters, strings, and arrays of characters can be written using the methods for a Writer, but these now use buffering to provide efficient writing of characters. In addition, the BufferedWriter class provides the method newLine() for writing the platform-dependent line separator.
The following code creates a PrintWriter whose output is buffered, and the characters are written using the UTF-8 character encoding (Figure 20.6(a)):
Figure 20.6 Buffered Writers
Charset utf8 = Charset.forName(“UTF-8”);
FileOutputStream outputFile = new FileOutputStream(“info.txt”);
OutputStreamWriter outputStream = new OutputStreamWriter(outputFile, utf8);
BufferedWriter bufferedWriter1 = new BufferedWriter(outputStream);
PrintWriter printWriter1 = new PrintWriter(bufferedWriter1, true);
The following code creates a PrintWriter whose output is buffered, and the characters are written using the default character encoding (Figure 20.6(b)):
FileWriter fileWriter = new FileWriter(“info.txt”);
BufferedWriter bufferedWriter2 = new BufferedWriter(fileWriter);
PrintWriter printWriter2 = new PrintWriter(bufferedWriter2, true);
Note that in both cases, the PrintWriter is used to write the characters. The Buffered-Writer is sandwiched between the PrintWriter and the underlying OutputStreamWriter (which is the superclass of the FileWriter class).