Java NIO.2 之Paths和Path

一、简介

Path是java.nio.file下的接口, 表示一个与平台无关的路径或者用于定位一个文件。它的功能与File有颇多相似的地方, 并且Path可以通过File toFile()方法返回一个File对象。

二、Paths

这个类的作用就是返回一个Path对象, 并且它只有2个静态方法。
-public static Path get(String first, String… more)
-public static Path get(URI uri)

通常情况下使用第一个方法, 它的好玩之处在于可以提供一个或多个参数.

// 提供一个参数
Path path = Paths.get("C:\\Program Files\\Java");
System.out.println(path);

// 输出:
C:\Program Files\Java
// 提供多个参数
Path path = Paths.get("C:", "Program Files", "Java");
System.out.println(path);

// 输出:
C:\Program Files\Java

三、Path

1.获取文件信息
Path path = Paths.get("G:", "myFile", "文件.txt");

// 获取路径名
String pathname = path.toString();

// 返回该路径中最远的元素
Path lastElement = path.getFileName();

// 返回一个绝对路径
Path absolutePath = path.toAbsolutePath();

// 返回路径的父目录
Path parent = path.getParent();

// 返回根目录
Path root = path.getRoot();

// 返回路径名中的路径数量(不包括根目录)
int elecount = path.getNameCount();

System.out.println(" 获取路径名: " + pathname +
        "\n 路径中最远的元素: " + lastElement +
        "\n 绝对路径: " + absolutePath +
        "\n 路径的父目录: " + parent +
        "\n 根目录: " + root +
        "\n path中的路径数量: " + elecount);

// 输出:
 获取路径名: G:\myFile\文件.txt
 路径中最远的元素: 文件.txt
 绝对路径: G:\myFile\文件.txt
 路径的父目录: G:\myFile
 根目录: G:\
 path中的路径数量: 2
2.对给定路径进行判断
path = Paths.get("G:\\myFile\\new");
Path path2 = Paths.get("G:\\myFile");

// 是否相等
int iseql = path.compareTo(path2);

// 文件路径是否以指定路径结尾
boolean endWith = path.endsWith("myFile");

// 路径是否以指定路径开始
boolean startWith = path.startsWith("G:\\");

// 获取文件系统
FileSystem fileSystem = path.getFileSystem();

// 获取第n个路径元素
int n = 0;
Path nPath = path.getName(n);

// 是否是绝对路径
boolean isabsolute = path.isAbsolute();

System.out.println(" path路径: " + path +
                "\n 路径path2: " + path2 +
                "\n 路径是否相等: " + (iseql == 0 ? "相等" : "不相等")  +
                "\n 路径 " + path + " 是否以指定路径结尾: " + endWith +
                "\n 路径 " + path + " 是否以指定路径开始: " + startWith +
                "\n 文件系统: " + fileSystem +
                "\n 路径" + path + "第" + n + "个路径元素为: " + nPath +
                "\n 路径" + path + "是否为绝对路径: " + isabsolute);

// 输出:
 path路径: G:\myFile\new
 路径path2: G:\myFile
 路径是否相等: 不相等
 路径 G:\myFile\new 是否以指定路径结尾: false
 路径 G:\myFile\new 是否以指定路径开始: true
 文件系统: sun.nio.fs.WindowsFileSystem@4be2cc
 路径G:\myFile\new第0个路径元素为: myFile
 路径G:\myFile\new是否为绝对路径: true
3.其它
Path preNormalize = Paths.get("G:\\myFile\\.\\.\\文件.txt");
System.out.println("消除路径 " + preNormalize + "消除相对路径元素后: " + preNormalize.normalize());

// 返回path的迭代器
Iterator<Path> pathIter = path.iterator();
while (pathIter.hasNext()) {
   System.out.println("路径" + path + "元素: " + pathIter.next());
}

// 给出两个路径之间的相对关系
Path pathArg = Paths.get("G:\\");
Path relative = path.relativize(pathArg);
System.out.println("路径"+ path + "与路径" + pathArg +"的关系:" + relative);

// 以当前路径为父路径, 根据给定路径解析新的路径,即使该路径其实并不存在
path = Paths.get("G:", "myFile");
path2 = Paths.get("myFile001");
System.out.println("解析后: " + path.resolve(path2));

// 以当前路径的上一级目录为父路径, 根据给定路径解析新的路径
System.out.println("解析后: " + path.resolveSibling(path2));

// 根据索引返回一个相对路径
Path path3 = path.subpath(0, 1);
System.out.println("路径" + path + "根据索引返回路径:" + path3);

// 输出:
消除路径 G:\myFile\.\.\文件.txt消除相对路径元素后: G:\myFile\文件.txt
路径G:\myFile\new元素: myFile
路径G:\myFile\new元素: new
路径G:\myFile\new与路径G:\的关系:..\..
解析后: G:\myFile\myFile001
解析后: G:\myFile001
路径G:\myFile根据索引返回路径:myFile
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值