Path
class, introduced in the JDK7 release, is the cornerstone of the java.nio.file
package. If your application uses file I/O, you will want to learn about the powerful features of this class.
Version Note: If you have pre-JDK7 code that usesjava.io.File
, you can still take advantage of thePath
class functionality by using theFile.toPath
method. See Legacy File I/O Code for more information.
As its name implies, the Path
class is a programmatic representation of a path in the file system. A Path
object contains the file name and directory list used to construct the path, and is used to examine, locate, and manipulate files.
A Path
instance reflects the underlying platform. In the Solaris OS, a Path
uses the Solaris syntax (/home/joe/foo
) and in Microsoft Windows, a Path
uses the Windows syntax (C:\home\joe\foo
). A Path
is not system independent. You cannot compare a Path
from a Solaris file system and expect it to match a Path
from a Windows file system, even if the directory structure is identical and both instances locate the same relative file.
The file or directory corresponding to the Path
might not exist. You can create a Path
instance and manipulate it in various ways: you can append to it, extract pieces of it, compare it to another path. At the appropriate time, you can check the existence of the file corresponding to the Path
, create the file, open it, delete it, change its permissions, and so on.
The Path
class is "link aware." Every Path
method either detects what to do when a symbolic link is encountered, or it provides an option enabling you to configure the behavior when a symbolic link is encountered.
The Path
class offers a rich feature set and is very easy to use. Most methods in the Path
class fall into one of the following two categories:
- Path operations – Methods for returning parts of a path, such as the root, name, or parent directory and methods for manipulating a path
- File operations – Methods for opening a file for I/O, creating a file, creating a directory, deleting a file, copying a file, and so on
No comments:
Post a Comment