Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

The ins and outs of standard input/output

Learn the basics of standard input, standard output, and standard error

  • Print
  • Feedback

Page 2 of 5

Suppose there are no ASCII codes in the key buffer. What happens? System.in.read() waits for the user to type keys and press the terminator. Under Windows, that terminator is the Enter key. Pressing Enter causes Windows to store a carriage return code (ASCII 13) followed by a new-line code (ASCII 10) in the key buffer. Therefore, the key buffer might contain several ASCII codes followed by a carriage return and a new-line character. The first of those codes returns from System.in.read(). Check out that activity by keying in, compiling, and running the Echo application; its source code appears in Listing 1.

Listing 1. Echo.java

// Echo.java
class Echo
{
   public static void main (String [] args) throws java.io.IOException
   {
      int ch;
      System.out.print ("Enter some text: ");
      while ((ch = System.in.read ()) != '\n')
         System.out.print ((char) ch);
   }
}


Echo completes the following steps:

  1. Calls the System.out.print() method, which takes a String argument, to output a prompt
  2. Calls System.in.read() to input ASCII codes from the standard input device as 32-bit integers
  3. Converts those 32-bit integers to 16-bit Unicode characters by way of the (char) cast
  4. Calls the System.out.print() method, which takes a char argument, to echo those Unicode characters to the standard output device


The last three steps in the previous four steps take place in a while loop, and continue until a new-line character is read. To run Echo so that it inputs from the keyboard and outputs to the screen, issue the following command line: java Echo.

Although System.in.read() never throws an exception (see the word-counting topic in this article for a definition of that term), when the standard input device is set to the keyboard, it might throw an exception when you redirect the standard input device from the keyboard to a file. For example, suppose you redirect the standard input device to a file, and System.in.read() reads content from the file. Now suppose that the file is situated on a floppy disk, and the user ejects that disk during the read operation. When the ejection takes place, System.in.read() throws an exception, informing the program that it can't read the file. That provides the reason for appending the throws java.io.IOException clause to the main() method header. (You will explore exceptions, throwing exceptions, and related concepts in a future article.)

How do you redirect the standard input device so that input originates from a file? The answer is to introduce a less-than sign, <, on the command line and follow that symbol with a filename. To see how that works, issue the following command line: java Echo <Echo.java. The command line redirects the standard input device to a file called Echo.java. When Echo runs, because each line ends in a new-line character, only the first line of text in Echo.java appears on the screen.

Suppose you need a utility program that reads an entire file and either displays the file's contents on the screen, copies those contents to another file, or copies those contents to a printer. Unfortunately, the Echo program only performs that task until it encounters the first new-line character. What do you do? The answer to the problem lies in the Type application. Listing 2 provides the source code:

  • Print
  • Feedback

Resources