首页 > 编程语言 > 详细

Java:Path与Paths

时间:2021-06-21 20:24:18      阅读:18      评论:0      收藏:0      [点我收藏+]

0、说明

用于读Path操作的接口(Path)与实现类(Paths)

1、模块:java.nio.file.Path、java.nio.file.Paths

2、方法

2.1、Paths

只有两个静态方法,用法:Paths.xxx( ... )

Path get( String first , String... more )

连接符把这些参数中的String连接起来,得到一个对应的Path(和Path.of( ... )相同

Windows中的连接符是\

Path get( URI uri ) 将一个给定的URI转化为Path对象

2.2、Path

2.2.1、静态方法,用法:Path.xxx( ... )

Path of( String first , String... more)

与Paths.get(...)作用相同

返回一个Path对象

Path of( URI uri )

2.2.2、实例方法,用法:p.xxx( ... )

返回值类型

方法

说明

int compareTo(Path other) 比较两个Path
boolean endsWith(String other) 判断该Path是否以other结尾
boolean endsWith(Path other) 判断该Path是否以另一个Path结尾
boolean equals(Object other) 判断该Path是否和另一个元素相等
Path getFileName() 文件名对应的Path
FileSystem getFileSystem() 返回构造了这个Path的文件系统
Path getName(int index)

返回Path某一级目录的名字

比如 usr\Docs\Test,对应0/1/2分别是usr、Docs、Test

int getNameCount() 返回Path目录有几级,常配合getName(int index)使用
Path getParent() 获取父目录的Path
Path getRoot() 获取根目录的Path
int hashCode() hash code
boolean isAbsolute() 判断该Path是否是绝对路径
Iterator<Path> iterator() 实现迭代器,允许对Path[ ]使用for each遍历
Path normalize() 将路径正规化(即转换为不含.和..的绝对路径)
WatchKey register(WatchService watcher, WatchEvent.Kind<?>... events)  
WatchKey register(WatchService watcher, WatchEvent.Kind<?>[] events, WatchEvent.Modifier... modifiers)  
Path relativize(Path other) 返回两个路径间的相对路径
Path resolve(String other)

如果other是绝对路径,返回other;

如果other是null,返回this;

否则,在this之后拼接other形成新Path

Path resolve(Path other)
Path resolveSibling(String other)

如果other是绝对路径,返回other;

如果other是null,返回this;

否则,在this的父目录下拼接other形成新的Path

Path resolveSibling(Path other)
boolean startsWith(String other) 判断该Path是否以other开头
boolean startsWith(Path other) 判断该Path是否以other开头
Path subPath(int start , int end) 返回从start到end的subpath
Path toAbsolutePath() 返回绝对路径
File toFile() Path→File
Path toRealPath(LinkOption... options)  
String toString() 返回该Path对应的String
URI toUri() 返回该Path对应的URI

正规路径:不含.和..的绝对路径

3、方法说明

最常用:

Path→String:p.toString()

Java:Path与Paths

原文:https://www.cnblogs.com/ShineLeBlog/p/14915249.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!