Java Reference
In-Depth Information
named in , out , and err . You've probably already seen out in Java programs
with statements like this:
System.out.println("Hello, world.");
You can also write:
System.err.println("Error message here\n");
and
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
while ((line = in.readLine()) != null) {
...
}
Java parallels Linux nicely on I/O descriptors. If you redirect any of those
file descriptors from the shell command line when you execute a Java program,
then that redirected I/O is available to your Java application—with no
additional work on your part.
In the example above, if you have System.in all wrapped up into a
BufferedReader from which your program is reading lines, then you can run
that program as:
$ java MyCode
and it will read input as you type it on your keyboard. This may be how you
test your program, but when you put this program to its intended use, you may
want it to be able to read from a file. This you can do without any change to
the program—thanks to file descriptors, input streams, and redirecting input,
for example:
$ java MyCode < file2
which will let the same Java program read from the file named file2 rather
than from keyboard.
Your Java program can also set the values of System.in , System.out ,
and System.err as it executes, to change their destinations.
One common example is changing the destination of System.out , the
typical recipient of debugging or logging messages. Say you've created a class
Search WWH ::




Custom Search