Reading Text from Standard Input


BufferedReader inStream = new BufferedReader (        new InputStreamReader(System.in)      ); String inLine = ""; while ( !(inLine.equalsIgnoreCase("quit"))) {     System.out.print("prompt> ");     inLine = inStream.readLine(); }



In a console program, it is common to read from the standard input, typically the command line. In this phrase, we show how you can read the standard input into a Java String variable.

Java contains three streams that are connected to the operating system streams. These are the standard input, standard output, and standard error streams. Respectively, they are defined in Java as the streams System.in, System.out, and System.err. We can make use of these streams to read or write to the operating systems standard input and output.

In our phrase, we create a BufferedReader to read from the System.in stream. Using this reader, we continue to read input lines from the standard input until the user types the word "quit."




JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

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