Input and Output in Java¶
System objects¶
The System class provides methods and objects that get input from the keyboard, print text on the screen, and do file input and output (I/O). System.out is the object that displays on the screen. When you invoke print and println, you invoke them on System.out.
You can even use System.out to print System.out:
System.out.println(System.out);
The result is:
java.io.PrintStream@80cc0e5
When Java prints an object, it prints the type of the object (PrintStream), the package where the type is defined (java.io), and a unique identifier for the object. On my machine the identifier is 80cc0e5, but if you run the same code you will probably get something different.
There is also an object named System.in that makes it possible to get input from the keyboard. Unfortunately, it does not make it easy to get input from the keyboard.
Keyboard input¶
First, you have to use System.in to create a new InputStreamReader.
InputStreamReader in = new InputStreamReader(System.in);
Then you use in to create a new BufferedReader:
BufferedReader keyboard = new BufferedReader(in);
Finally you can invoke readLine on keyboard, to take input from the keyboard and convert it to a String.
String s = keyboard.readLine();
System.out.println(s);
There is only one problem. There are things that can go wrong when you invoke readLine, and they might throw an IOException. A method that throws an exception has to include it in the prototype, like this:
public static void main(String[] args) throws IOException {
// body of main
}
File input¶
Here’s a program that reads lines from a file and prints them:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.io.*;
public class Words {
public static void main(String[] args)
throws FileNotFoundException, IOException {
processFile("words.txt");
}
public static void processFile(String filename)
throws FileNotFoundException, IOException {
FileReader fileReader = new FileReader(filename);
BufferedReader in = new BufferedReader(fileReader);
while (true) {
String s = in.readLine();
if (s == null) break;
System.out.println(s);
}
}
}
|
This first line imports java.io, the package that contains FileReader, BufferedReader, and the rest of the elaborate class hierarchy Java uses to do common, simple things. The * means it imports all classes in the package.
Here’s what the same program looks like in Python:
for word in open('words.txt'):
print word
I’m not kidding. That’s the whole program, and it does the same thing.
Catching exceptions¶
In the previous example, processFile can throw FileNotFoundException and IOException. And since main calls processFile, it has to declare the same exceptions. In a larger program, main might declare every exception there is.
The alternative is to catch the exception with a try statement. Here’s an example:
1 2 3 4 5 6 7 8 | public static void main(String[] args) {
try {
processFile("words.txt");
} catch (Exception ex) {
System.out.println("That didn't work. Here's why:");
ex.printStackTrace();
}
}
|
The structure is similar to an if statement. If the first “branch” runs without causing an Exception, the second branch is skipped.
If the first branch causes an Exception, the flow of execution jumps to the second branch, which tries to deal with the exceptional condition (by saying “error” in a polite way). In this case it prints an error message and the stack trace.
You can download this code from Words.java and the word list from words.txt. Make sure both files are in the same folder. (If you are using an IDE like NetBeans or Eclipse, make sure the words.txt file is in your project directory.)