«^»
2.9. Output and input

2.9.1. Attaching an output stream

To write values to a file called results, you can use a variable of the type PrintWriter. Suppose you want to use a variable called filout:

0173: PrintWriter filout = 
0174:       new PrintWriter(new BufferedWriter(new FileWriter("results")));
This declaration assumes that the following imports appear at the start of the file of source code:
0175: import java.io.BufferedWriter;
0176: import java.io.FileWriter;
0177: import java.io.PrintWriter;
If you want some output to be sent to the standard output, which is usually the screen, you can use System.out as an output stream. The variable out is a variable (of the type java.io.PrintStream) that is a ‘class variable’ of the class System which is defined in the package java.lang. Any class of this package is automatically available to a program without the need for any import declaration.

2.9.2. Outputting values to the output stream

In order to output a textual representation of a value, the print method should be applied to an object of the class PrintWriter or PrintStream. Here are two examples:

0178: filout.print("Hello World!");
0179: System.out.print("Hello World!");
The print method can be passed: If you want the output to move on to the next line after the value has been printed, use println instead of print.

2.9.3. Closing a file

As output to a BufferedWriter stream is buffered, the stream will need to be closed when you have finished using it:

0182: filout.close();

2.9.4. Attaching an input stream

To read values, you will need a variable of the type BufferedReader that is in the package java.io. To read from the standard input, which is normally the keyboard, you can use System.in as an input stream. So, if you want to use a variable called input for this input stream, you can use the following declaration:

0183: BufferedReader input = 
0184:       new BufferedReader(new InputStreamReader(System.in));
This declaration assumes that the following imports appear at the start of the file of source code:
0185: import java.io.BufferedReader;
0186: import java.io.InputStreamReader;

If, instead, you want to read values from a file called data, you can use:

0187: BufferedReader filin = new BufferedReader(new FileReader("data"));
This declaration assumes that the following imports appear at the start of the file of source code:
0188: import java.io.BufferedReader;
0189: import java.io.FileReader;

2.9.5. Reading a line of characters

The method readLine can be used to read in a line of characters from an input stream. It returns a value of type String. Here are two examples:

0190: String inputLine = input.readLine();
0191: String filinLine = filin.readLine();
The class String is defined in the package java.lang, and so it can be used in a program without the need for an import declaration.

2.9.6. Reading a value

If you would like to read a value into a variable whose type is one of the primitive types, you first need to call readLine to read in a line of characters and then call an appropriate method to parse the string. Here is an example where a value of type int is obtained from the keyboard:

0192: String line = input.readLine();
0193: int intVal = Integer.parseInt(line);
This can be abbreviated to:
0194: int intVal = Integer.parseInt(input.readLine());

Given a variable called line containing a String:

0195: String line = input.readLine();
values of the other primitive types can be obtained using the following statements:
0196: long longVal = Long.parseLong(line);
0197: float floatVal = Float.parseFloat(line);
0198: double doubleVal = Double.parseDouble(line);
0199: boolean booleanVal = new Boolean(line).booleanValue();
Note that the methods parseFloat and parseDouble were introduced into Java when the Java 2 Platform was released, and so, if you are using JDK 1.0.2 or JDK 1.1.x, you will have to use methods called floatValue or doubleValue instead (in a similar way in which booleanValue is used above).

The classes Integer, Long, Float, Double and Boolean are defined in the package java.lang, and so they can be used in a program without the need for any import declarations.

2.9.7. Handling more than one data item per line

You can use the class java.util.StringTokenizer if you want more than one data item per line. Suppose a line contains an int, followed by a float, followed by another int. You could use:

0200: String line = input.readLine();
0201: StringTokenizer tokens = new StringTokenizer(line);
0202: String token = tokens.nextToken();
0203: int firstInt = Integer.parseInt(token);
0204: token = tokens.nextToken();
0205: float theFloatVal = Float.parseFloat(token);
0206: token = tokens.nextToken();
0207: int secondInt = Integer.parseInt(token);

2.9.8. Flushing the output

If you want the user to type on the same line as a prompt, you will need to flush the output stream after outputting the prompt:

0208: BufferedReader input = 
0209:       new BufferedReader(new InputStreamReader(System.in));
0210: System.out.print("Type in an integer: ");
0211: System.out.flush();
0212: String line = input.readLine();
0213: int value = Integer.parseInt(line);

2.9.9. Dealing with java.io.IOException

If you are going to use the classes and methods from the java.io package, you will find that you are unable to compile your program unless it indicates what you want to happen if an exception called java.io.IOException occurs. Details about ‘exception handling’ are given later. So, to begin with, you may be happy for your program to crash if an IO exception occurs. This can be done by adding the clause throws IOException to the heading of any method that does IO. For example:

0214: public static void main(String[ ] args) throws IOException { ... }
This code assumes that the following import appears at the start of the file of source code:
0215: import java.io.IOException;

2.9.10. JDK Version 1.0.x

Many of the classes given above are not available if you use Version 1.0.x of the JDK. And the input-output facilities provided by Version 1.0.x can only handle byte streams. Those of JDK Version 1.1.x (and later) include support for character streams, i.e., streams containing 16-bit Unicode characters rather than just 8-bit bytes.