Java Reference
In-Depth Information
The root is D:\
TryPath.java is a file containing 2121 bytes.
D:\Beginning Java SE 7\Projects\TryPath\src\TryPath.java was created
2011-03-23T18:33:22.315038Z
D:\Beginning Java SE 7\Projects\TryPath\src\TryPath.java was last
accessed
2011-05-08T10:48:55.974509Z
D:\Beginning Java SE 7\Projects\TryPath\src\TryPath.java was last
modified
2011-05-08T10:48:55.976509Z
D:\Beginning Java SE 7\Projects\TryPath\src\TryPath.java is 2121
bytes.
How It Works
The analysis of a path is carried out in the static checkPath() method so I'm explaining that first. The
first statement in this method calls the getNameCount() for the Path object that is passed as the argu-
ment to obtain the number of elements in the path. The if statement that follows calls isAbsolute() for
the Path object to determine if the object contains an absolute or a relative path. Up to this point there
has been no interaction with the file system. All operations just involve the Path object.
If the path is not absolute, the toAbsolutePath() method for the Path object is called to convert it. This
does access the file system and has the potential to throw a SecurityException if you are not permitted
to access the file.
Next the static notExists() method in the Files class is called to see whether path references
something real. This obviously involves accessing the file system. If the path does not reference a file or
directory that exists, the checkPath() method returns after outputting the fact.
Having established that path refers to a real file or directory, the static readAttributes() method in the
Files class is called with path as the first argument to obtain the attributes for whatever Path references.
The method then calls the isDirectory() method for the BasicFileAttributes object to determine if
path references a directory. The second argument determines that the basic file attributes are retrieved.
You use the BasicFileAttributes object to decide whether path references a file or a directory and to
obtain various properties of the file or directory.
The main() method calls checkPath() for three different paths, with the results that you see. You should
supply your own path specifications in this example to suit your particular environment.
Other Path Operations
The resolve() method for a Path object reference resolves the current path, which should be a directory,
against the Path object that is supplied as the argument and returns the resultant Path object. Typically the
current path is an absolute path and the argument is a relative path, and the result is the path that is produced
by joining the argument path to the current path. For example, if the current path is C:\Temp and the argu-
ment path is output.txt , the result is the path C:\Temp\output.txt . If the argument is an absolute path,
the argument path is returned, and if the argument is null , the current Path object is returned. You use this
method later in this chapter when you get to copy files.
Search WWH ::




Custom Search