THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

23.1. The System Class


The System class provides static methods to manipulate system state and acts as a repository for
system-wide resources. System provides functionality in four general areas:



  • The standard I/O streams

  • Manipulating system properties

  • Utilities and convenience methods for accessing the current Runtime

  • Security


You'll learn about security in more detail in Section 23.5 on page 677.


23.1.1. Standard I/O Streams


The standard input, output, and error streams are available as static fields of the System class.


public static final InputStreamin

Standard input stream for reading data.

public static final PrintStreamout

Standard output stream for printing messages.

public static final PrintStreamerr

Standard error stream for printing error messages. The user can often redirect
standard output to a file. But applications also need to print error messages
that the user will see even if standard output is redirected. The err stream is
specifically devoted to error messages that are not rerouted with the regular
output.

For historical reasons, both out and err are PrintStream objects, not PrintWriter objects. See
"Print Streams" on page 525 for a discussion about PrintStream and PrintWriter types.


Although each of the standard stream references is declared final, the static methods setIn, setOut, and
setErr allow you to redefine the actual streams to which these references are bound (by using native code to
bypass the language level restriction of not assigning final variables). These methods are security checked
and throw SecurityException if you do not have permission to change the standard streams.


23.1.2. System Properties


System properties define the system environment. They are stored by the System class in a Properties
object (see page 620). Property names consist of multiple parts separated by periods. For example, here is a
dump of the standard properties on one system:


[View full width]


Standard System Properties


java.version=1.5.0_02
java.vendor=Sun Microsystems Inc.
java.vendor.url=http://java.sun.com/
java.vm.specification.version=1.0
java.vm.specification.vendor=Sun Microsystems Inc.
java.vm.specification.name=Java Virtual Machine Specification

Free download pdf