Although not really needed for simple console
I/O, the InputStreamReader
and OutputStreamWriter
classes nicely illustrate stream wrapping concepts. The
System.out
and System.in
use 8-bit encoded character streams. Java version 1.1 made
available 16-bit streams that are compatiable with the 2-byte
Unicode
encoding, which is used within Java for all character representations.
To provide for 16-bit streams and other capabilities,
the example below wraps the System.in
stream with an InputStreamReader
and the System.out
stream with an OutputStreamWriter.
|
import java.io.*;
/**
* Demonstrate reading characters from
the console using
* the standard input from System.in wrapped
with
* InputStreamReader.
**/
public class SingleCharIOApp
{
public static void main (String arg[])
{
char ch;
int tmp = 0;
// System.in std input stream
already opened by default.
// Wrap in a new reader stream
to obtain 16 bit capability.
InputStreamReader reader =
new InputStreamReader (System.in);
// System.out std output stream
already opened by default.
// Wrap in a new writer stream
to obtain 16 bit capability.
OutputStreamWriter writer
= new OutputStreamWriter (System.out);
try {
// Hit CTRL-Z
on PC's to send EOF, CTRL-D on Unix.
// The EOF puts
-1 = 0xFFFFFFFF into integer.
while (tmp !=
-1 ) {
//
Read a single character from keyboard
tmp =
reader.read ();
//
A 1 byte character is returned in integer.
//
Cast to char to get character in low 2 bytes.
ch
= (char) tmp;
//
If a symbol character with 2 byte unicode read in,
//
the will see 4 hex output
writer.write
("echo " + ch
+
" whose code = "+ Integer.toString (tmp,16) + '\n');
//
Need to flush the OutputStream buffer to get the
//
characters sent to the screen.
writer.flush
();
}
}
catch (IOException e) {
System.out.println
("IO error:" + e );
}
} // main
} // class SingleCharIOApp
|
If there is an end of file (EOF),
then -1
= 0xFFFFFFFF is held by the integer, thus using
all four bytes. The table below shows typical output.
Example
Case |
c:\>
java SingleCharIO_App1
abc123+- <= user input
echo a whose code = 61
echo b whose code = 62
echo c whose code = 63
echo 1 whose code = 31
echo 2 whose code = 32
echo 3 whose code = 33
echo + whose code = 2b
echo - whose code = 2d
whose code = d
echo
whose code = a |
The hex values shows the codes according
to the default encoding for the particular host OS system
on which it was run (may look different when run on your system.)
For the standard ASCII characters the hex values are the same.
Note: this DOS
program does line returns with "\r\n". Thus
the program reads the return key hit as two more characters.
The "\r" causes the cursor to move all the
way to the left, which is why there is no echo seen for it
above. The "\n"causes the line change.
PrintWriter
The OutputStreamWriter
class only has a few basic write()
methods. We can obtain many print(..)
and println() overloaded methods with the PrintWriter
class. (See the API
Specifications for the PrintWriter
class.)
So in the following example we wrap the OutputStreamWriter
instance with a PrintWriter
and thus obtain use of its methods. We also take advantage
of the autoflushing switch in one of the PrintWriter
constructors so that we do not have to flush the buffers explicitly
ourselves after every print.
This program produces similar output as the previous
program.