Java:Path与Paths
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()
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· 百万级群聊的设计实践
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
· 永远不要相信用户的输入:从 SQL 注入攻防看输入验证的重要性