Java Reference
In-Depth Information
Of course, the operations with a reader are much more efficient if you buffer it with a BufferedReader
object like this:
BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in));
Here, you wrap an InputStreamReader object around System.in and then buffer it using a
BufferedReader object. This makes the input operations much more efficient. Your read operations are
from the buffer belonging to the BufferedReader object, and this object takes care of filling the buffer from
System.in when necessary via the underlying InputStreamReader object.
A CharArrayReader object is created from an array and enables you to read data from the array as though
it were from a character input stream. A StringReader object class does essentially the same thing, but ob-
tains the data from a String object.
Using Writers
The main subclasses of the Writer class are as shown in Figure 8-5 .
FIGURE 8-5
Let's discuss just a few details of the most commonly used of these classes.
The OutputStreamWriter class writes characters to an underlying binary stream. It also has a subclass,
FileWriter , that writes characters to a stream encapsulating a file. Both of these are largely superseded by
the new I/O facilities.
Note that the PrintWriter class has no particular relevance to printing, in spite of its name. The
PrintWriter class defines methods for formatting binary data as characters and writing it to a character
stream. It defines overloaded print() and println() methods that accept an argument of each of the prim-
itive types, of type char[] , of type String , and of type Object . The data that is written is a character rep-
resentation of the argument. Numerical values and objects are converted to a string representation using the
static valueOf() method in the String class. Overloaded versions of this method exist for all the primitive
types plus type Object . In the case of an argument that is an Object reference, the valueOf() method just
 
 
Search WWH ::




Custom Search