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:
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
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
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.
"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
print methods. Similarly, the JVM provides access for reading
input from the keyboard with an instance of 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,
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,
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.
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.
Latest update: Nov. 10, 2004