Using the Standard IO Streams


Using the Standard I/O Streams

A second way to read and write program data is to use the standard I/O streams. If you remember from Chapter 6, the standard input and output streams are static variables defined in the System class. The standard input stream is System.in . The standard output stream is System.out . Since the standard I/O streams are public static variables from the java.lang package, you can access them anywhere in any program.

The standard input stream is an instance of the InputStream class, which is a byte stream. The default is for the standard input to come from the keyboard. The standard input stream has access to the read() method that is used to read a byte or an array of bytes. It is often convenient to wrap a character stream such as an InputStreamReader around the standard input stream allowing you to read character or String data directly.

The standard output stream is an instance of the PrintStream class, which is a byte output stream. The default for the standard output is to write to the console. The standard output stream has access to the print() and println() methods , which write a String or a String representation of a primitive value or object to the output stream. The standard output stream and the print() and println() methods have been used hundreds of times in the examples in this book.

The main advantage of using the standard I/O streams is that a lot of the work has been done for you. You don't need to create an I/O stream object. There are no input files to parse or create. One disadvantage is that there is no permanent record of what you have done. Unless you write a script of some sort , you must type in all of the input every time you run the program and the output will similarly not be saved.

To demonstrate the use of standard I/O we will write a class named StdIODemo . The main() method of this class creates a USatm76 object using the standard input stream to acquire the necessary inputs from the keyboard. In order to read the inputs as character data, an InputStreamReader is wrapped around the standard input stream. A BufferedReader is wrapped around the InputStreamReader to give access to the readLine() method that can read a line of input at a time.

The user is prompted for the units and altitude inputs. When the input is typed in and the Enter or Return key is pressed, the readLine() method returns the line of input as a String . The String corresponding to the altitude is converted to type double before it is passed to the USatm76 constructor. After the USatm76 object is created, the corresponding atmospheric conditions are printed to the console using the standard output stream and the println() method.

The StdIODemo class source code is shown next .

 import java.io.*; public class StdIODemo {   public static void main(String args[]) {     BufferedReader reader;     String units, altitude;     //  Wrap a BufferedReader and InputStreamReader     //  around the standard input stream.     try {       reader = new BufferedReader(                  new InputStreamReader(System.in));     //  The user is prompted for the system of units     //  and the altitude.     System.out.print("Select units (SI or English):  ");       units = reader.readLine();       System.out.print("Select altitude (m or ft):  ");       altitude = reader.readLine();       reader.close();       //  Create a USatm76 object with the recently       //  acquired input values.       USatm76 atm =        new USatm76(units, Double.parseDouble(altitude));       //  Print out the results using the standard       //  output stream       String label[] = atm.getLabels();       System.out.println("\ngeometric altitude    = " +                           atm.getAltitude() + label[0]);       System.out.println("geopotential altitude = " +              atm.getGeoPotentialAltitude() + label[1]);       System.out.println("temperature           = " +                       atm.getTemperature() + label[2]);       System.out.println("pressure              = " +                           atm.getPressure() + label[3]);       System.out.println("molar mass            = " +                         atm.getMolarMass() + label[4]);       System.out.println("density               = " +                           atm.getDensity() + label[5]);     } catch (IOException ioe) {        System.out.println("IO Exception occurred");        System.exit(1);     }   } } 

Output (will vary depending on the input provided) ”

 Select units (SI or English): SI Select altitude (m or ft) : 20000.0 geometric altitude    = 20000.0 m geopotential altitude = 19937.27227876952 m temperature           = 216.65 K pressure              = 5529.256361823237 N/m^2 molar mass            = 0.0289645 kg/mole density               = 0.08890932913275061 kg/m^3 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

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