Java Reference
In-Depth Information
Path dataFile = Paths.get(
System.getProperty("user.home"), "dir", "output.txt");
On my system this defines the following path:
C:\Users\Ivor\dir\output.txt
Of course, this in no way guarantees that the
dir
subdirectory exists. To be sure, you need to check that
it does. You see how to do this, and how to create files and directories, a little later in this chapter.
If you would like to see what the full set of standard system properties are, you can find the complete
list in the JDK documentation for the static
getProperties()
method in the
System
class. You can also re-
trieve the current set of properties on your system and their values by calling the
getProperties()
method,
so let's put a little program together to do that.
TRY IT OUT: Getting the Default System Properties
Here's the program to list the keys and values for the current set of system properties on your computer:
public class TryProperties {
public static void main(String[] args) {
java.util.Properties properties = System.getProperties();
properties.list(System.out);
}
}
TryProperties.java
This outputs all the system properties.
How It Works
The
getProperties()
method returns the set of system properties encapsulated in an object of type
Properties
. The
Properties
class is defined in the
java.util
package, and I chose to use the fully
qualified class name rather than an import statement for it. You call the
list()
method for the
Proper-
ties
object to output the properties to the stream that is passed as the argument, in this case
System.out
,
which corresponds to your display screen.
Setting System Properties
As I discussed in the previous section, after you know the key for a system property you can obtain its value,
which is a
String
object, by calling the static
getProperty()
method in the
System
class with the key
for the property you are interested in as the argument. You can also change the value for a system property
by calling the static
setProperty()
method in the
System
class. The
setProperty()
method expects two