java.nio.file.Path


public interface Path
extends Comparable<Path>, Iterable<Path>, Watchable
1. A Path represents a path that is hierarchical and composed of a sequence of directory and file name elements separated by a special separator or delimiter. 2. A root component, that identifies a file system hierarchy, may also be present.3. The name element that is farthest from the root of the directory hierarchy is the name of a file or directory. The other name elements are directory names.4. A Path can represent a root, a root and a sequence of names, or simply one or more name elements.5. A Path is considered to be an empty path if it consists solely of one name element that is empty.6. Accessing a file using an empty path is equivalent to accessing the default directory of the file system. 

method classify:1.  Path defines the getFileNamegetParentgetRoot, and subpath methods to access the path components or a subsequence of its name elements.2.  In addition to accessing the components of a path, a Path also defines the resolve and resolveSibling methods to combine paths. 3.  The relativize method that can be used to construct a relative path between two paths. Paths can be compared,     and tested against each other using the startsWith and endWith methods.4.  This interface extends Watchable interface so that a directory located by a path can be registered with a WatchService and entries in the directory watched.

accessing files:1. Paths may be used with the Files class to operate on files, directories, and other types of files.For example, suppose we want a BufferedReader to read text from a file "access.log". The file is located in a directory "logs" relative to the current working directory and is UTF-8 encoded.
1 Path path = FileSystems.getDefault().getPath("logs", "access.log");
2 BufferReader reader = Files.newBufferedReader(path, StandardCharsets.UTF_8);
Interoperability:1. The toPath method may be used to obtain a Path from the abstract path name represented by a java.io.File object.   The resulting Path can be used to operate on the same file as the java.io.File object. 2. In addition, the toFile method is useful to construct a File from the String representation of a Path.
concurrency:   Implementations of this interface are immutable and safe for use by multiple concurrent threads.

method:
方法签名 意义
int compareTo(Path other)  按顺序比较两个路径
boolean endsWith(Path other)  测试一个Path对象是否以给定的path为结尾
boolean endsWith(String other)  测试一个Path对象是否以给定的字符串为结尾,字符串会转换为Path对象
boolean equals(Object other)  
Path getFileName()  
FileSystem getFileSystem()  返回创建Path对象的文件系统对象  
Path getName(int index)  以Path对象返回路径对象中一个名字组成
int getNameCount()  Returns the number of name elements in the path.
Path getParent()  Returns the parent path, or null if this path does not have a parent.
Path getRoot()  Returns the root component of this path as a Path object, or null if this path does not have a root component.
boolean isAbsolute()  Tells whether or not this path is absolute.
Iterator<Path>  iterator()  Returns an iterator over the name elements of this path.
Path normalize()  移除诸如. 和.. 等冗余的路径元素。

WatchKey

register(WatchService watcher, WatchEvent.Kind<?>... events)

 Registers the file located by this path with a watch service.
WatchKey register(WatchService watcher, WatchEvent.Kind<?>[] events, WatchEvent.Modifier... modifiers)  Registers the file located by this path with a watch service.
Path relativize(Path other)  返回用this 进行解析,相对于other 的相对路径。
Path resolve(Path other)  如果other 是绝对路径,那么就返回other ;否则,返回通过连接this 和other 获得的路径。
Path resolve(String other)  如果other 是绝对路径,那么就返回other ;否则,返回通过连接this 和other 获得的路径。
Path resolveSibling(Path other)  如果other 是绝对路径,那么就返回other ;否则,返回通过连接this 的父路径和other 获得的路径。
Path resolveSibling(String other)  如果other 是绝对路径,那么就返回other ;否则,返回通过连接this 的父路径和other 获得的路径。
boolean startsWith(Path other)  类似endsWith
boolean startsWith(String other)  类似endsWith
Path subpath(int beginIndex, int endIndex)  Returns a relative Path that is a subsequence of the name elements of this path.
Path toAbsolutePath()  Returns a Path object representing the absolute path of this path.
File toFile()  Returns a File object representing this path.
Path toRealPath(LinkOption... options)  Returns the real path of an existing file.
URI toUri()  Returns a URI to represent this path.
时间: 2024-12-20 07:58:20

java.nio.file.Path的相关文章

java 7 使用java.nio.file.*操作文件

java7中辅助类Files新增了两个方法用于读去文件的全部行和全部字节.So..再也不用缓冲区了. 1 package java8_test; 2 3 import java.io.IOException; 4 import java.nio.file.Files; 5 import java.nio.file.Path; 6 import java.nio.file.Paths; 7 import java.util.List; 8 9 public class TestMain { 10

java.nio.file.InvalidPathException: Illegal char &lt;:&gt;

一.报错: java.nio.file.InvalidPathException: Illegal char <:> at sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182) at sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153) at sun.nio.fs.WindowsPathParser.parse(WindowsPathParse

docker elasticsearch挂载宿主机报 java.nio.file.AccessDeniedException: /usr/share/elasticsearch/data/nodes

① docker启动 es实例 docker run --name elasticsearch -p 9200:9200 -p 9300:9300 \ -e "discovery.type=single-node" \ -e ES_JAVA_OPTS="-Xms64m -Xmx128m" \ -v /mydata/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasti

Jenkins迁移jobs后项目构建出现java.nio.file.DirectoryNotEmpt

1.首先说一下迁移过程1)新环境部署Jenkins持续集成环境,这里不在详细说明安装过程:2)直接把旧的Jenkins家目录下得jobs目录通过scp得方式进行传输到新得环境下; 迁移完成后启动新环境Jenkins服务,构建项目控制台输出如下: 观察控制台输出到最后,项目是构建成功得,而且不影响运行. 2.错误原因通过Google查找得知有人提交了这个issues,This started happening on a new Jenkins server that a teammate was

java.nio.file.FileSystemException: D:\kafka_2.12-2.1.0\kafka_2.12-2.1.0\logs\__consumer_offsets-30\00000000000000000000.timeindex.cleaned: 另一个程序正在使用此文件,进程无法访问。

在启动kafka时候报错: 原因是因为在logs中文件有问题,那么可以删除配置文件 文件夹下文件,还有就是重新建立一个文件夹 原文地址:https://www.cnblogs.com/xiufengchen/p/10960951.html

2.2.5 NIO.2 Path 和 Java 已有的 File 类

NIO与IO交互 toPath() File -- Path toFile() Path -- File Demo: import java.io.File; import java.nio.file.Path; import java.nio.file.Paths; public class PathAndFile { public static void main(String[] args) { File file = new File("/Users/jinxing/Documents/

Java NIO Path

The Java Path interface is part of the Java NIO 2 update which Java NIO received in Java 6 and Java 7. The Java Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of

5种调优Java NIO和NIO.2的方式

Java NIO(New Input/Output)——新的输入/输出API包——是2002年引入到J2SE 1.4里的.Java NIO的目标是提高Java平台上的I/O密集型任务的性能.过了十年,很多Java开发者还是不知道怎么充分利用NIO,更少的人知道在Java SE 7里引入了更新的输入/输出 API(NIO.2).这篇教程展示了5个在Java编程的一些常见场景里使用NIO和NIO.2包的简单示例. NIO和NIO.2对于Java平台最大的贡献是提高了Java应用开发中的一个核心组件的

Java NIO try-with-resources [ 转载 ]

Java NIO try-with-resources [ 转载 ] @原文链接 http://blog.csdn.net/jackiehff/article/details/17765909     try-with-resources语句是一个声明一个或多个资源的 try 语句.一个资源作为一个对象,必须在程序结束之后随之关闭. try-with-resources语句确保在语句的最后每个资源都被关闭 .任何实现了 java.lang.AutoCloseable的对象, 包括所有实现了 ja