Path Interface

Package: java.nio.file

The Path interface defines an object that represents a file or directory path. Note that a Path object doesn’t represent the file or directory indicated by the path; it represents the path to the file. The file or directory pointed to by a Path object may or may not actually exist on disk.

Because Path is an interface, not a class, it has no constructors. Of the many ways to create a Path object, one of the most common is to use the get method of the Paths class. For more information, see Paths Class.

Path objects can also be created by static members of the Files class. For more information, see Files Class.

Remember.eps Yes, it’s confusing that there is an interface named Path and a class named Paths, and separate classes named File and Files. At times, it seems like “Confusing” is Java’s middle name.

A Path object can be thought of as a root component and a hierarchical sequence of distinct names separated by separator characters (usually backslashes). The root component identifies the file system being used; in a Windows system, this might be a drive letter. The sequence of names represents the directories needed to navigate through the file system to the target file or directory. The last name in the sequence represents the name of the target file or directory itself.

Methods

Method

Description

Path getFileName()

Returns the name of the target file or directory.

Path getName(int index)

Returns the name at the specified level in the path hierarchy.

int getNameCount()

Returns the number of name elements in the path.

Path getRoot()

Returns the root element of the path.

Iterator<Path> iterator()

Returns an Iterator that can be used to iterate each name element in the path. The existence of this method allows you to use Path objects in an enhanced for statement (for example, foreach).

File toFile()

Returns a File object representing the file pointed to by this Path object.

String toString()

Returns the string representation of the path.

Remember.eps For most file processing applications, you probably won’t use any of the methods of the Path interface. Instead, you’ll use Path objects in conjunction with static methods of the Files class. For more information, see Files Class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset