Java Reference
In-Depth Information
Table 9.1
Input stream examples.
Source
Stream wrappings
Console
BufferedReader (new InputStreamReader (System.in))
Text in a byte stream
BufferedReader (new InputStreamReader (InputStream))
Text file
BufferedReader (new FileReader (new File ("f.txt"))
Binary Data File
DataInputStream (new FileInputStream (new File ( " f.bin " ))
Table 9.2
Output stream examples.
Destination
Stream wrappings
Console
PrintWriter (new OutputStreamWriter (System.out))
Text in a byte stream
PrintWriter (new BufferedWriter (new OutputStreamWriter
(OutputStream)))
Text file
PrintWriter (new BufferedWriter (new FileWriter (new File
("f.txt")))
Binary Data File
DataOutputStream (new FileOutputStream (new File ("f.bin"))
There are a number of other issues regarding serialization that are beyond the
scope of this chapter and topic. More discussion and example programs are
available in the Web Course: Chapter 9 and serialization also is a technique
important for the distributed computing discussions in Part II.
9.9 Choosing a stream class
All of these stream classes for text and binary data, for console and files, can get
rather confusing. To help decide what stream classes and wrappers to use for a
particular kind of data, we provide two tables. Table 9.1 suggests input stream
combinations and Table 9.2 suggests output streams according to the type of data
and its source or destination. You can check these tables to find the starting class
for your data I/O and possible wrapper classes that add buffering, filtering, and
other useful methods.
9.10 Primitive types to bytes and back
We have seen that Java is very strict with data types. Every data element must be
assigned a specific type. Casting can convert one type into another but a data value
can never simultaneously act as more than one type. This differs from C/C
++
Search WWH ::




Custom Search