Java Nio 简明教程
Java NIO - Path
顾名思义,路经是文件系统中诸如文件或目录之类的实体所在特定位置,以便人们可以在该特定位置搜索和访问它。
As name suggests Path is the particular location of an entity such as file or a directory in a file system so that one can search and access it at that particular location.
从技术上讲,在 Java 的术语中,路经是一个在 Java 7 版本中引入到 Java NIO 文件包中的接口,并且是特定文件系统中位置的表示。由于路经接口在 Java NIO 包中,因此它的限定名称为 java.nio.file.Path。
Technically in terms of Java, Path is an interface which is introduced in Java NIO file package during Java version 7,and is the representation of location in particular file system.As path interface is in Java NIO package so it get its qualified name as java.nio.file.Path.
一般来说,实体的路经可以是两種類型,一种是绝对路徑,另一種是相对路徑。正如两种路徑的名称所示,绝对路徑是从根目錄到該实体所在位置的位置地址,而相對路徑是相对于其他一些路徑的位置地址。Path在其定义中使用分隔符,在 Windows 中使用“\”,而在 UNIX 操作系统中使用“/”。
In general path of an entity could be of two types one is absolute path and other is relative path.As name of both paths suggests that absolute path is the location address from the root to the entity where it locates while relative path is the location address which is relative to some other path.Path uses delimiters in its definition as "\" for Windows and "/" for unix operating systems.
为了获取 Path 的实例,可以使用 java.nio.file.Paths 类` get() `的静态方法。此方法将路经字符串或一串连接后形成路经字符串的字符串转换为 Path 实例。如果传递的参数包含非法字符,此方法还会抛出运行时 InvalidPathException。
In order to get the instance of Path we can use static method of java.nio.file.Paths class get().This method converts a path string, or a sequence of strings that when joined form a path string, to a Path instance.This method also throws runtime InvalidPathException if the arguments passed contains illegal characters.
如上所述,可以通过传递根元素和查找文件所需的完整目录列表来检索绝对路径。而可以通过将基路径与相对路径结合来检索相对路径。将在以下示例中说明如何检索这两个路径
As mentioned above absolute path is retrieved by passing root element and the complete directory list required to locate the file.While relative path could be retrieved by combining the base path with the relative path.Retrieval of both paths would be illustrated in following example
Example
package com.java.nio;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.file.FileSystem;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathDemo {
public static void main(String[] args) throws IOException {
Path relative = Paths.get("file2.txt");
System.out.println("Relative path: " + relative);
Path absolute = relative.toAbsolutePath();
System.out.println("Absolute path: " + absolute);
}
}
到目前为止,我们知道什么是路径接口,为什么我们需要它以及如何访问它。现在,我们会了解到路径接口为我们提供哪些重要的方法。
So far we know that what is path interface why do we need that and how could we access it.Now we would know what are the important methods which Path interface provide us.
Important methods of Path Interface
-
getFileName() − Returns the file system that created this object.
-
getName() − Returns a name element of this path as a Path object.
-
getNameCount() − Returns the number of name elements in the path.
-
subpath() − Returns a relative Path that is a subsequence of the name elements of this path.
-
getParent() − Returns the parent path, or null if this path does not have a parent.
-
getRoot() − Returns the root component of this path as a Path object, or null if this path does not have a root component.
-
toAbsolutePath() − Returns a Path object representing the absolute path of this path.
-
toRealPath() − Returns the real path of an existing file.
-
toFile() − Returns a File object representing this path.
-
normalize() − Returns a path that is this path with redundant name elements eliminated.
-
compareTo(Path other) − Compares two abstract paths lexicographically.This method returns zero if the argument is equal to this path, a value less than zero if this path is lexicographically less than the argument, or a value greater than zero if this path is lexicographically greater than the argument.
-
endsWith(Path other) − Tests if this path ends with the given path.If the given path has N elements, and no root component, and this path has N or more elements, then this path ends with the given path if the last N elements of each path, starting at the element farthest from the root, are equal.
-
endsWith(String other) − Tests if this path ends with a Path, constructed by converting the given path string, in exactly the manner specified by the endsWith(Path) method.
Example
以下示例说明了上面提到的 Path 接口的不同方法 −
Following example illustartes the different methods of Path interface which are mentioned above −
package com.java.nio;
import java.io.IOException;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.file.FileSystem;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathDemo {
public static void main(String[] args) throws IOException {
Path path = Paths.get("D:/workspace/ContentW/Saurav_CV.docx");
FileSystem fs = path.getFileSystem();
System.out.println(fs.toString());
System.out.println(path.isAbsolute());
System.out.println(path.getFileName());
System.out.println(path.toAbsolutePath().toString());
System.out.println(path.getRoot());
System.out.println(path.getParent());
System.out.println(path.getNameCount());
System.out.println(path.getName(0));
System.out.println(path.subpath(0, 2));
System.out.println(path.toString());
System.out.println(path.getNameCount());
Path realPath = path.toRealPath(LinkOption.NOFOLLOW_LINKS);
System.out.println(realPath.toString());
String originalPath = "d:\\data\\projects\\a-project\\..\\another-project";
Path path1 = Paths.get(originalPath);
Path path2 = path1.normalize();
System.out.println("path2 = " + path2);
}
}