Java Reference
In-Depth Information
TIP To create a symbolic link in Windows 7 from the command line you must open a
command-line window with administrator privileges. To do this, select the Start button and
enter Command in the search box. Right-click Command Prompt in the list of results and se-
lect Run as Administrator from the pop-up. You then have a command window in which you
can execute the MKLINK command.
Accessing System Properties
In some circumstances you might want to specify a path that is specific to the current environment. In this
case, accessing one of the system properties can help. A system property specifies values for parameters re-
lated to the system environment in which your program is executing. Each system property is identified by
a unique name and has a value associated with the name that is defined as a string. A set of standard system
properties is always available, and you can access the values of any of these by passing the name of the prop-
erty that you are interested in to the static getProperty() method that is defined in the java.lang.System
class. The value of the property is returned as type String .
For example, the directory that is currently the default base for relative paths is defined by the property
that has the name "user.dir" , so you can access the path to this directory with the following statement:
String currentDir = System.getProperty("user.dir");
The "file.separator" property identifies the character used as an element separator in a path on the
current system. You can retrieve this with the statement:
String separator = System.getProperty("file.separator");
You could then use the results of the previous two fragments to specify explicitly where the file with the
name "output.txt" is located:
Path dataFile = Paths.get(currentDir + separator + "output.txt");
Of course, this is equivalent to just specifying the file name as the relative path, so you have not achieved
anything new, other than using the separator specific to the current system. Of course, you could use multiple
string arguments to the get() method and have the separator characters inserted for you.
Another system property with the name "user.home" has a value that defines a path to the user's home
directory. You could therefore specify that the "output.txt" file is to be in this directory as follows:
Path dataFile = Paths.get(System.getProperty("user.home"), "output.txt");
The location of the user's home directory is system dependent, but wherever it is you can access it in this
way without building system dependencies into your code.
Naturally, you could specify the second argument to the constructor here to include directories that are to
be subdirectories of the home directory. For instance:
Search WWH ::




Custom Search