Home : Course Map : Chapter 9 : Java :
Console I/O
JavaTech
Course Map
Chapter 9

Introduction
Overview
Streams
Wrappers,Buffers
Console I/O
  Text Output 
     Demo 1

  Formatter/printf()
     Demo 2

  Tex 2t Input
     Demo 3

  Scanner
     
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
   Demo13
Object I/O
Types to Bytes
Stream Filters
Other I/O Topics
Exercises

    Supplements
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
     Exercises
     Feedback
     References
     Resources
     Tips
     Topic Index
     Course Guide
     What's New

The term console refers to a command line interface to the host platform or other environment, such as a browser, for the JVM. (The browser console only provides for text output.) In this day of GUI predominance, some consider console I/O anachronistic. However, Java is intended for many different platforms and for some of these a console is usually the most convenient, or perhaps the only way, for a user to interact with a program. (See Chapter 24 on embedded Java and other small platforms.)

The JVM automatically creates the streams:

  System.in  - keyboard input
  System.out
- console output
  System.err
- console output for error messages.

These are built on deprecated 8-bit streams. The constructors were deprecated but not the classes or methods. So one can still use, for example, System.out.println(..) without the deprecation warning message on compilation.

Internally, Java uses only Unicode (see Character Encoding ) encoding of characters. Unicode provides two bytes for each character. It provides a much larger character set than the ancient one byte ASCII representation and so allows for internationalization of text.

Java 1.0, however, provided only for 8-bit encoded character I/O. Java 1.1 upgraded to 16-bit character I/O but to remain compatible with previous code, the 8-bit streams remain and are still used for keyboard I/O. The adapter classes InputStreamReader and OutputStreamWriter were provided to convert the 8-bit streams to 16-bit.

Below we first further discuss the System streams and then in the following sections we look at the Reader/Writer streams for console I/O of text.

System.in & System.out

"Standard" input/output streams are provided by default by the JVM for the environment in which it runs. For applications, these streams would correspond to the keyboard and user console such as a DOS window. For a browser, an applet will send standard output to a Java console window.

So far in the course we sent output to the console using System.out print methods. Similarly, the JVM provides access for reading input from the keyboard with an instance of System.in. However, System.in is an instance of the base level InputStream class and doesn't provide convenient higher level methods like those provided by System.out, a PrintStream class.

With System.in you can only read in a single byte, or an array of byte types, which returns as an int value. You must then cast each byte from the keyboard input into a char type. So, for example,

  try {
    int tmp  = System.in.read ();
    char c = (char)tmp;
  }
  catch (IOException e)
  {}

where, as for all Java I/O operations, you must catch the exception thrown for any I/O problem.

If the byte corresponds to an 8-bit encoded character, then it will be in the low order byte of the integer and you can cast it to char. The System.out, a PrintStream object, also corresponds to an 8-bit stream.

Note that in addition to the standard input and output streams, the JVM also provides by default the System.err stream for error messages, which typically connects to the user's console. For a browser, applet error messages are sent to the browser's Java console window.

References & Web Resources

 

Latest update: Nov. 10, 2004

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

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

  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
21
22 23 24
Supplements

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.