Java Reference
In-Depth Information
Notice the File class used throughout the java.io package, typically as a parameter or
return value type. It is a good programming design to use File objects instead of Strings to
represent files. This allows you to perform certain checks on the files, similar to those done
in the FileDemo program, before using the File for I/O operations. Be sure to check the
documentation of the File class for a complete list of the methods in the class.
Low-Level and High-Level Streams
In the previous section, I discussed the difference between streams and read-
ers and writers. I will now discuss the details of using the various stream and
reader/writer classes. I am going to begin with input and output streams,
which can be separated into two categories:
Low-level streams. An input or output stream that connects directly to a
data source, such as a file or socket.
High-level streams. An input or output stream that reads or writes to
another input or output stream.
You can tell which streams are low-level and which streams are high-level
by looking at their constructors. The low-level streams take in actual data
sources in their constructors, while the high-level streams take in other
streams. For example, FileOutputStream is a low-level stream, and each of its
constructors takes in a variation of a filename. The DataOutputStream is a
high-level stream, and it only has one constructor, which takes in an existing
output stream:
public DataOutputStream(OutputStream out)
In other words, the only way to create a FileOutputStream is to provide a
filename, while the only way to create a DataOutputStream is to have an exist-
ing OutputStream already.
Low-Level Streams
The following is a list of the low-level input and output streams in the java.io
package and their corresponding data source:
FileInputStream and FileOutputStream.
For writing and reading binary
data from a file.
ByteArrayInputStream and ByteArrayOutputStream.
For writing and
reading data from an array of bytes.
Search WWH ::




Custom Search