Home : Course Map : Chapter 9 : Java :
I/O Overview
Course Map
Chapter 9

Console I/O
  Text Output 
     Demo 1

     Demo 2

  Tex 2t Input
     Demo 3

Demo 4
File Class
  File I/O
  File Output-Text
     Demo 5

  Formatter to File
     Demo 6

  File Input - Text
    Demo 7

  Scanner - Files
     Demo 8

  File I/O - Binary
     Demo 9
   Demo 10
File Chooser Dialog
  Demo 11

Character Codes
  Demo 12
Object I/O
Types to Bytes
Stream Filters
Other I/O Topics

Character I/O
  Demo 1   Demo 2
Random Access
  Demo 3
ZIP/GZIP Streams
  Demo 4
Piped Streams
  Demo 5
NIO Framework
More NIO
  Demo 6

     About JavaTech
     Codes List
     Topic Index
     Course Guide
     What's New

Java I/O seeks to provide a consistent framework that works the same essential way regardless of the sort of I/O involved. That framework centers around the concept of the stream, which is a sequential flow of bytes in one direction. For example, an output stream carries text to the console and an input stream brings text from the keyboard. An output stream carries data to a file and an input stream brings data from a file. An input stream brings in data from a network socket. An output stream sends objects to a distant computer on the network.

The bulk of Java I/O classes belong to the java.io package. The class heirarchies build on the base classes InputStream, OutputStream, Reader and Writer to provide a wide range of input and output tasks. Also, there are stream classes that wrap a stream to add more capabilties to it.

This diagram shows most of the members of the java.io package. Rectangles are classes,
parallelograms are abstract classes, and ovals are interfaces.

Some I/O classes provide a destination or source, such as a file or an array. Others process the stream in some way such as buffering or filtering the data. In this many of these classes in this chapter.

Packages involving I/O include:

  • java.io - the primary Java I/O classes (see above diagram.)
  • java.nio, java.nio.* - a new set of packages with Java 1.4 that uses the concept of channels that represent an open connection to a hardware device, file, or other entity. The classes provide various features such as buffering and multiplexing among different channels. Channels don't supplant streams but rather work with them to add additional capabilities and enhanced scaling when working with large numbers of connections.
  • java.net - I/O over the network
  • java.util.zip - methods for reading from and writing to ZIP and GZIP files.
  • java.util.jar - methods for reading from and writing to JAR (Java Archive) files.
  • javax.imageio, javax.imageio.* - these packages deal with image I/O, including the encoding/decoding of images in particular formats.

Java I/O Challenges

Unfortunately, when one first encounters Java I/O, it seems to violate a primary goal of Java - try to be 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 obtaining input from the console.

So far in this course, we skipped much of this confusion by simply sending text output to the console with the System.out.println() method. This static method uses the System.out "standard" output stream, which the JVM opens by default; no user setup is required. The System.out object is an instance of PrintStream, which provides fairly high level methods. There is also a standard input stream, the System.in class. However, it only provides low level methods that require several steps to convert a console input to a string.

As we discussed in Chapter 5: Tech section, the PrintStream class now has a printf() method that works in a manner quite like a function of the same name in C/C++. The new method uses internally an instance of the Formatter class, added in J2SE5.0, that provides C/C++ style numerical formatting capabilities. The printf()and also the class Scanner, used for reading formatted input, help considerably with simplifying and expanding the range of basic I/O tasks.

We discuss these new tools in this chapter. We provide lots of examples of other types of I/O tools and techniques as well, so that one can find a template for whatever I/O task you need to do.

References & Web Resources

Latest update: Nov. 10, 2004

Histogram I/O
Hist I/O - Get/Set
  Demo 1
Hist I/O - Objects
  Demo 2
  Demo 3
Filtering Data
  Demo 4

Physics Model
Simulation Design
Physics Simulator
  Demo 1
Experiment Design
Experiment Sim.
  Demo 2
Expt. + Analysis
  Demo 3

  Part I Part II Part III
Java Core 1  2  3  4  5  6  7  8  9  10  11  12 13 14 15 16 17
18 19 20
22 23 24

1  2  3  4  5  6  7  8  9  10  11  12

Tech 1  2  3  4  5  6  7  8  9  10  11  12
Physics 1  2  3  4  5  6  7  8  9  10  11  12

Java is a trademark of Sun Microsystems, Inc.