Java Reference
In-Depth Information
or even a whole package of classes that write log messages to System.out
(e.g., System.out.println("some message") ). Now you realize that you'd
like the output to go somewhere else.
You could redirect standard out , as in:
$ java SomeClass > log
but that requires the user to remember to redirect the output every time the
program is invoked. That's fine for testing, or if the output is intended to go
to a different place each time it is invoked. But, in this example, we always want
the output to go to the same location.
Without changing any of the System.out.println() statements, all the
messages can be sent to a new location by reassigning the System.out print
stream. The System class has a setter for out —that is, a method which will let
you set a new value for out . In your Java program, open the new destination
file and give this to the System class:
PrintStream ps = new PrintStream("pathname");
System.setOut(ps);
It will be used from that point forward in the execution of this program as its
out output stream.
CAUTION
Changing standard out (or in , or err ) will make the change for all classes
from here on in this invocation of the Java runtime—they are static fields of
the one System class. Since this is so serious a move, the Java Security
Manager (see Section 5.8.4.2) provides a check for setIO to see if the Java
program is allowed to make such changes. If such a security manager is in
place and you are not allowed to make such changes, an exception
( SecurityException ) will be thrown. Note also that the permission applies
to setting any of the fields; it doesn't divide the permission into setting one
(e.g., out ) but not another (e.g., in ).
4.3.2
When Linux programs are run they have the open file descriptors described
above. They also carry with them a list of “name=value” pairs called their
environment . These environment variables allow for context to be shared among
Environment Variables
Search WWH ::




Custom Search