Path相关核心API

1、Paths作为工具类,简化Path的创建过程。通过FileSystem创建平台相关的Path。
2、FileSystems作为工具类,简化FileSystemProvider,以及相应FileSystem的创建过程。
1)获取默认系统参数、Uri、Path的FileSystemProvider
2)通过provider获取FileSystem
3、UnixPath实现在构造时
1)移除多余的/,并检查字符的有效性
2)默认UTF-8编码路径字符串为字节数组,用于后续的Path操作
Path操作
- 操作Path字符串本身,不访问文件系统
String toString();将Path转换为字符串形式
Path getFileName();root最远一级的目录或文件
Path getName(int index);以 / 分组,以0开始,获取指定所有的name
int getNameCount();以 / 分组,name的个数
Path subpath(int beginIndex, int endIndex);不包括根路径,包括beginIndex,不包括endIndex的name组成的Path
Path getParent();除去FileName的路径
Path getRoot();unix的root目录就是 /
Path toAbsolutePath();1、绝对路径转换后不变;2、相对路径转换后为工作目录/相对路径
Path resolve(String other);1、other是相对路径追加到原路径的后面;2、other是绝对路径,则直接返回
Path relativize(Path other);path导航到other路径的操作步骤
- 访问文件系统,若不存在则抛出异常
Path toRealPath(LinkOption... options) throws IOException;1、解析符号链接的实际Path;2、相对路径转为绝对路径;3、解析冗余路径:.(当前目录) 或 ..(父目录)
外部存储扩展
FileSystemProvider,作为SPI可以由第三方实现。如果有第三方外部存储的场景,可以自定义Scheme,来对应相应的FileSystemProvider。同时,在调用FileSystemProvider的public abstract Path getPath(URI uri)方法时,建议遵循Path的工厂定义规范,委派给相应的FileSystem实现来获取平台相关的Path。