Home : Course Map : Chapter 9 : Java :
Wrappers & Buffers
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

The Java I/O framework uses wrapper classes to build specialized I/O streams. Conceptually the aim is to provide great flexibility and modularity by wrapping streams with classes that provide particular capabilities as needed.

Typically an instance of a lower level class is created and then it is wrapped inside more specialized stream instances by passing it to the wrapper via a constructor argument. The following code segment gives a couple of examples:

 // Convert the 8-bit System input to 16-bit
 InputStreamReader in = new InputStreamReader (System.in);

 // Wrap with in a buffer object to speed I/O
 BufferedReader bufIn = new BufferedReader (in);

 // Read the keyboard input using the readLine method
 String strLine = bufIn.readLine ();



Here we first wrap an 8-bit character stream, System.in, with a 16-bit character Reader class. This provides for efficienct input of non-ASCII characters.

Buffered classes improve the performance of I/O by providing intermediate data storage buffers. The data must fill the buffer to a certain level before it is sent to the next stage, thus performing fewer time-consuming operations. Note that this can require the "flushing" of data near the end of the transmission when the data did not reach the level required for release.

In the above example, we wrap the InputStreamReader with a BufferedReader, which not only buffers the data but also provides some useful methods such as readLine(). With this method the input text from the command line returns as a string rather than one character at a time as in the lower level console classes.

Note: this use of the term wrapper has nothing to do with the numerical wrapper classes, which wrap primitive type values, but follow the same pattern of adding capabilities to an object.




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.