Java Reference
In-Depth Information
a focus on text and numerical data going to and from the console and disk files.
Part II deals with I/O over networks, and Chapter 23 deals with I/O via serial
and parallel ports. The Web Course Chapter 9: Supplements section looks at the
java.nio capabilities.
9.1.1 Java I/O challenges
Unfortunately, when one first encounters Java I/O it seems to violate a primary
goal of Java - provide simpler and more transparent code than the C and C
++
languages ! The elegant abstraction that is so powerful when dealing with complex
I/O tasks can seem clumsy and overly complicated for basic tasks such as writing
and reading text with the console.
Until now we avoided the complexities by sending text output to the con-
sole with the System.out.println() method. The System.out object is
defined in the java.lang.System class. It is an instance of PrintStream ,
which provides higher level methods such as println() than the basic stream
classes. The JVM automatically opens this standard output stream so no user setup
is required. The System.in object, also defined in java.lang.System is an
instance of InputStream ,which only provides low-level methods and requires
extra steps to convert input to a string. We discuss console I/O in Section 9.4 for
text and numbers. These and other features of Java I/O can seem rather confusing
at first. We will provide lots of examples here and in the Web Course so that one
can find guides for many different kinds of I/O tasks.
As we discussed in Chapter 5, Java separated formatting of text and its output
until J2SE 5.0, which introduced the printf() method into the PrintStream
class. This method provides C/C
style formatting with similar format speci-
fiers. In addition, there is a new Scanner class with methods to read formatted
text from the console and from files.
++
9.2 Streams
The primary conceptual component of the Java I/O framework is the stream:
stream = a one way sequential flow of bytes
Input operations begin by opening a stream from the source and then invoking
a read() method, or an overloaded version of it, to obtain the data. Similarly,
output operations begin by opening a stream to the destination and then using a
write() method to send the data.
The base stream classes are the abstract classes:
InputStream -byte input stream base class
OutputStream -byte output stream base class
Reader - 16-bit character input stream base class
Writer - 16-bit character output stream base class
Search WWH ::




Custom Search