411 REFERENCE · Wikibooks
The Way of the Java/Java IO
Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.
Input and Output in Java
System objects
System is the name of the built-in class that contains
methods and objects used to get input from the keyboard,
print text on the screen, and do file input/output (I/0).
System.out is the name of the object we have used
to print text. When you invoke print and println,
you invoke them on the object named System.out.
Interestingly, you can print System.out:
verbatim
System.out.println (System.out);
verbatim
The output is:
verbatim
java.io.PrintStream@80cc0e5
verbatim
As usual, when Java prints an object, it prints the type
of the object, which is PrintStream, the package
in which that 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 has
type BufferedInputStream. System.in 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.
verbatim
InputStreamReader in = new InputStreamReader ("System.in");
verbatim
Then you use in to create a new BufferedReader:
verbatim
BufferedReader keyboard = new BufferedReader (in);
verbatim
The point of all this manipulation is that there is a method
you can invoke on a BufferedReader, called readLine,
that gets input from the keyboard and converts it into a
String. For example:
verbatim
String s = keyboard.readLine ();
System.out.println (s);
verbatim
reads a line from the keyboard and prints the result.
There is only one problem. There are things that can go wrong
when you invoke readLine, and they might cause an
IOException. There is a rule in Java that if a method might
cause an exception, it should say so. The syntax looks
like this:
verbatim
public static void main (String[] args) throws IOException
verbatim
This indicates that main might throw an IOException.
You can think of throwing an exception as similar to throwing
a tantrum.
File input
Reading input from a file is equally stupid. Here is an
example:
verbatim
public static void main (String[] args)
throws FileNotFoundException, IOException
processFile ("/usr/dict/words");
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);
verbatim
This program reads each line of the named file (/usr/dict/words)
into a String and then prints the line. Again, the declaration
throws FileNotFoundException, IOException is required by the
compiler. The object types FileReader and BufferedReader
are part of the insanely complicated class hierarchy Java uses to do
incredibly common, simple things. Other than that, you couldn't
possibly care how this program works.