Java Reference
In-Depth Information
In general, the pathname that you use to create a Path object has two parts: an optional prefix or root element
followed by a series of names separated by the system default separator character for pathnames. Under MS
Windows the prefix for a path on a local drive is a string defining the drive, such as "C:\\" or "C:/" . Under
UNIX the prefix is a forward slash, "/" . A path that includes a prefix is an absolute path and a path without
a prefix is a relative path . The last name in a path can be a directory name or a file name. All other names
must be directory names.
The pathnames I have used in the preceding code fragments have all been absolute paths, because I in-
cluded the drive letter in the path for Windows; a forward slash identifies a root directory in a UNIX system.
You can check whether a path object is an absolute path by calling its isAbsolute() method:
Path path = FileSystems.getDefault().getPath(
"C: /Program Files (x86)/Java /jdk1.7.0/src/java/nio/file");
if(path.isAbolute())
System.out.println("Path is absolute.");
This fragment outputs the message confirming that path is absolute.
If you omit the root element in a path specification, you have a relative path and the pathname string is
interpreted as a path relative to the current directory. This implies that you can reference a file that is in the
same directory as your program by just the file name.
For example:
Path myFile = Paths.get("output.txt");
This statement creates a Path object encapsulating a pathname string that is just the name "output.txt" .
This is interpreted as being the name of a file in the current directory when the Path object is used. Unless
it has been changed programmatically, the current directory is the directory that was current when program
execution was initiated. You see in a moment how you can obtain the absolute path from a Path object, re-
gardless of how the Path object was created.
You could also refer to a file in a subdirectory of the current directory using a relative path:
Path myFile = Paths.get("dir", "output.txt");
If the current directory is C:\Projects\Test , the myFile object references the path to the file
C:\Projects\Test\dir\output.txt . Thus, you can use a relative path specification to reference files in
the current directory, or in any subdirectory of the current directory.
Symbolic Links
Some operating systems support the use of symbolic links in a file or directory path. A symbolic link is a
special kind of file that contains a relative or absolute path to another file. The path in a symbolic link is
usually just a string specifying the path, and a Path element is identified as a symbolic link by an attribute
to the element. A symbolic link appears as the last element in a path and acts as a transparent redirection
mechanism for accessing a file or directory.
Microsoft Windows supports symbolic links. You can use the MKLINK function from the command line
under Microsoft Windows to create a symbolic link if you have administrator privileges. I discuss file attrib-
utes a little later in this chapter and I mention symbolic links again in that context.
Search WWH ::




Custom Search