Java Programming, Lecture Notes # 34, Revised 10/03/99
Students in Prof. Baldwin's Introductory Java Programming classes at ACC are responsible for knowing and understanding all of the material in this lesson (except that they are not responsible for detailed information that is specific to C++).
The detailed material on C++ is provided as supplementary material for the benefit of those persons who are already familiar with C++ and who are making the transition into Java.
What is a stream?
Both Java and C++ support the notion of input and output streams. In both cases, the notion represents a stream of bytes entering into the program from an external source, or a stream of bytes flowing from the program to an external sink.
What is standard I/O?
The standard input and standard output stream notion is based on the concept of working on a system that has a standard input device and a standard output device.
On DOS and UNIX systems, the default standard input device is the keyboard, but it can be redirected to a different device at the operating system level.
Likewise, the default standard output device is the screen, but it can be redirected at the operating system level to another device.
What is I/O redirection?
DOS users will be familiar with the following command that causes a directory listing to be produced and stored in a disk file named director.txt.
dir > director.txt |
This is a DOS command that redirects the standard output to a device other than the screen: namely to a disk file.
The read() method
The Java read() method can read and return a single byte from the standard input device and store that byte in an integer. It returns an integer value of -1 if it encounters an end-of-file (eof).
Why store it in an int?
Storing the byte into the low end of an integer, makes it possible for the program to distinguish between the integer value of -1 for eof and the 8-bit character consisting of the bits
11111111
This would be interpreted as -1 if the interpretation were being made on eight bits alone.
What is a keyboard eof?
An eof can be simulated on a DOS system keyboard by holding down the ctrl key and pressing the z key.
Likewise, an eof can be simulated on a UNIX system keyboard by holding down the ctrl key and pressing the d key at the beginning of a line.
Other systems may have different ways of simulating an eof from the keyboard.
Reading a stream of bytes from the keyboard
The following Java code fragment will read a stream of bytes from the standard input device until encountering an eof.
while(System.in.read() != -1){ //do something }//end while loop |
What is the OOP structure involved here?
This code fragment accesses the read() method of the object referred to by the class variable named in of the class named System.
Displaying characters on the standard output device
Likewise, the following two code fragments will each display a string argument on the Java standard output device.
System.out.println("String argument") System.out.print("String argument") |
What is the OOP structure involved here?
In the first case, the code fragment accesses the println() method of the object referred to by the class variable named out of the class named System. In the second case, the print()method is accessed.
What is the difference between the two?
The difference between the two is that the println() method automatically inserts a newline at the end of the string argument whereas the print() method leaves the display cursor at the end of the string argument. (This is somewhat analogous to the use of WriteLn and Write in Pascal.)
How is this done in C++?
Although both Java and C++ embrace the concept of stream input/output, the mechanism for achieving input/output is significantly different between the two, with C++ still supporting the older non-object-oriented style of C and also providing a new object-oriented style peculiar to C++. |
Do we need another example of Java I/O?
Several examples of Java input/output have been presented in previous lessons, so there is no need to provide another one here.
The tip of the iceberg
Stream I/O is a fairly complex topic in Java. We will have a lot more to say about stream I/O in subsequent lessons. The above introduction was simply the tip of the iceberg.
Character streams (as opposed to byte streams)
The stream I/O capability of Java was enhanced to deal with 16-bit Unicode characters in the JDK 1.1 update to the development kit. The following statement was extracted from a JavaSoft document that discusses the new features added to the JDK 1.1 version.
IO Enhancements The I/O package has been extended with character streams, which are like byte streams except that they contain 16-bit Unicode characters rather than eight-bit bytes. Character streams make it easy to write programs that are not dependent upon a specific character encoding, and are therefore easy to internationalize. Nearly all of the functionality available for byte streams is also available for character streams. |
How can you identify a character stream?
The classes that support this 16-bit functionality are easy to spot. They usually have the word reader or writer in their names. We will also be discussing this new functionality in a subsequent lesson.
Q - The Java read() method reads and returns a single byte from the standard input device. It stores that byte according to what type. What does the method return if the user enters an eof?
A - The Java read() method reads and returns a single byte from the standard input device and stores that byte in an integer. It returns an integer value of -1 if the user enters an eof.
Q - What keystroke combination can be used to simulate an eof at the keyboard of a DOS system?
A - An eof can be simulated on a DOS system keyboard by holding down the ctrl key and pressing the z key.
Q - Provide a Java code fragment illustrating how you would read a stream of bytes from the standard input device until encountering an eof and quit reading when the eof is encountered. Explain in object-oriented terms how your code fragment works.
A - The following Java code fragment will read a stream of bytes from the standard input device until encountering an eof.
while(System.in.read() != -1) { //do something } |
This code fragment accesses the read()method of the object referred to by the class variable named in of the class named System.
Q - Provide a Java code fragment illustrating two different ways to display a String argument on the Java standard output device. Explain how your code works in object-oriented terms. Make certain that you explain the difference between the two.
A - The following two code fragments will each display a string argument on the Java standard output device.
System.out.println("String argument") System.out.print("String argument") |
In the first case, the code fragment accesses the println() method of the object referred to by the class variable named out of the class named System. In the second case, the print() method is accessed instead of the println() method.
The difference between the two is that the println() method automatically inserts a newlineat the end of the string argument whereas the print() method leaves the display cursor at the end of the string argument.
-end-