Java Reference
In-Depth Information
arguments; the first is a
String
object identifying the property to be changed, and the second is a
String
object that is the new property value.
You also have the possibility to remove the current value that is set for a property by using the static
clearProperty()
method in the
System
class. You just pass a string specifying the property key as the
argument. It is possible that the Java security manager may not permit this operation to be carried out, in
which case an exception of type
SecurityException
is thrown. The
clearProperty()
method throws an
exception of type
NullPointerException
if the argument is
null
, or an exception of type
IllegalArgu-
mentException
if you pass an empty string as the argument.
For a specific example of where you might want to set a system property, suppose that you want to change
the specification for the system property that specifies the current working directory. That property has the
key
"user.dir"
, so you could use the following statement:
System.setProperty("user.dir", "C:/MyProjects");
Executing this statement changes the current working directory to
"C:/MyProjects"
. Now when you are
using a relative path, it is relative to this directory. You can change the system property that defines the cur-
rent directory as often as you like in your program, so you can always adjust the current directory to be the
one containing the file you are working with if that is convenient. Of course, when you do this, you need to
be sure that the directory does exist, so it is wise to verify that the directory is there before executing the call
to the
setProperty()
method. That sort of verification is the next topic of this chapter.
Testing and Checking Path Objects
First of all, you can get information about a
Path
object by using the methods for the object shown in
Table
METHOD DESCRIPTION
getName(int n)
Returns the nth name element in this path as a
Path
object. Path elements are indexed from 0 start-
ing with the root element or element that is closest to the root in a relative path. The method throws
an
IllegalArgumentException
if
n
is not valid for the path.
getFileName()
Returns the name of the file or directory referenced by this path as a
Path
object. This is the name
element furthest from the root element.
getNameCount()
Returns the number of elements in the path as a value of type
int
.
isAbsolute()
Returns
true
if the
Path
object refers to an absolute pathname and
false
otherwise.
Returns a relative path that is formed by the sequence of path elements from
beginIndex to en-
dIndex-1
. The method throws an exception of type
IllegalArgumentException
if either argument
is not a valid index for the path.
subpath( int
beginIndex,
int endIndex)
Returns
true
if the current path starts with the path specified by the argument and
false
otherwise.
An overload of this method accepts a
String
argument.
startsWith(
Returns
true
if the current path ends with the path specified by the argument and
false
otherwise.
An overload of this method accepts a
String
argument.
endsWith(
Returns a
Path
object containing the path for the parent directory of the file or directory represented
by the current
Path
object. This is the original path without the last name. The method returns
null
if there is no parent specified.
getParent()
Returns the root element of the current
Path
object as a
Path
object, or
null
if this
Path
object does
not have a parent.
getRoot()