Redirecting Standard InputOutput

   

Redirecting Standard Input/Output

To support the standard input and output devices (usually the keyboard and screen), Java defines two stream objects that you can use in your programs without having to create stream objects of your own. The System.in object ( instantiated from the InputStream class) enables your programs to read data from the keyboard, whereas the System.out object (instantiated from the PrintStream class) routes output to the computer's screen. You can use these stream objects directly to handle standard input and output in your Java programs, or you can use them as the basis for other stream objects you might want to create.

These streams, like other streams, can be redirected to cause the input and output to be read from or written to something other than the default source. The default behavior for these two streams in shown in Listing 21.1.

Listing 21.1, for example, is a Java application that accepts a line of input from the user and then displays the line onscreen.

Listing 21.1 Source Code for IOApp.java
 import java.io.*; class IOApp {   public static void main(String args[])   {     String inputStr = "";     byte buffer[] = new byte[255];     System.out.println("Type a line of text. Type 'exit'to quit the program.");     // Keep looping until the user types exit     while( inputStr.indexOf( "exit" ) == -1 )     {       try       {         System.in.read(buffer, 0, 255);         inputStr = new String( buffer );         System.out.print("The line you typed was: ");         System.out.println(inputStr);       }       catch ( IOException ex )       {         System.out.println( ex );       }     }   } } 

To redirect these streams to another source, you can create a new stream that wraps one of the System.in or System.out streams and does something different with it. For example, you could take the System.out stream and redirect it to a file. Listing 21.2 shows how this could be done.

Listing 21.2 Source Code for RedirectExample.java
 import java.io.*; import java.sql.Timestamp; class RedirectExample {   // Change this path if you are using Unix or want another path   public static final String FILE = "c:\systemin.txt";   public static void main(String args[])   {     try     {       // Create a file output stream and set the append = true flag       FileOutputStream outStr = new FileOutputStream( FILE, true );       // Create a print stream so that you can connect to System.out       PrintStream printStream  = new PrintStream( outStr );       // Tell System.out to redirect to the printStream that you've created       System.setOut( printStream );       // Print out some text. It should not go to the console, but to the       // text file created above.       Timestamp now = new Timestamp( System.currentTimeMillis() );       System.out.println( now.toString() + ": This is text that should go to the file" );      // Close the open Resources       outStr.close();       printStream.close();     }     catch( FileNotFoundException ex )     {       ex.printStackTrace();       System.exit( -1 );     }     catch( IOException ex )     {       ex.printStackTrace();       System.exit( -1 );     }   } } 
   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net