Java Reference
In-Depth Information
Use random-access files
Use character streams
Use character streams for file I/O
Apply Java's type wrappers to convert numeric strings
S ince the beginning of this topic, you have been using parts of the Java I/O system, such
as println( ) . However, you have been doing so without much formal explanation. Because
the Java I/O system is based upon a hierarchy of classes, it was not possible to present its
theory and details without first discussing classes, inheritance, and exceptions. Now it is
time to examine Java's approach to I/O in detail.
Be forewarned, Java's I/O system is quite large, containing many classes, interfaces, and
methods. Part of the reason for its size is that Java defines two complete I/O systems: one
for byte I/O and the other for character I/O. It won't be possible to discuss every aspect
of Java's I/O here. (An entire book could easily be dedicated to Java's I/O system!) This
chapter will, however, introduce you to the most important and commonly used features.
Fortunately, Java's I/O system is cohesive and consistent; once you understand its funda-
mentals, the rest of the I/O system is easy to master.
Before we begin, an important point needs to be made. The I/O classes described in this
chapter support text-based console I/O and file I/O. They are not used to create graphical
user interfaces (GUIs). Thus, you will not use them to create windowed applications, for
example. However, Java does include substantial support for building graphical user inter-
faces. The basics of GUI programming are found in Chapter 15 , where applets are intro-
duced; Chapter 16 , which offers an introduction to Swing; and Chapter 17 , which presents
an overview of JavaFX. (Swing and JavaFX are two of Java's GUI toolkits.)
Java's I/O Is Built upon Streams
Java programs perform I/O through streams. An I/O stream is an abstraction that either
produces or consumes information. A stream is linked to a physical device by the Java I/O
system. All streams behave in the same manner, even if the actual physical devices they are
linked to differ. Thus, the same I/O classes and methods can be applied to different types
of devices. For example, the same methods that you use to write to the console can also be
used to write to a disk file. Java implements I/O streams within class hierarchies defined in
the java.io package.
Search WWH ::




Custom Search