检索有关路径的信息

可以使用 Path 对象的方法获取有关路径的信息:

  • toString() 返回路径的字符串表示形式

     placeholderCopyPath p1 = Paths.get("/var/www"); // p1.toString() returns "/var/www"
  • getFileName() 返回文件名(或者更具体地说,是路径的最后一个元素

     placeholderCopyPath p1 = Paths.get("/var/www"); // p1.getFileName() returns "www"
    Path p3 = Paths.get("C:\\Users\\DentAr\\Documents\\HHGTDG.odt"); // p3.getFileName() returns "HHGTDG.odt"
  • getNameCount() 返回构成路径的元素数

     placeholderCopyPath p1 = Paths.get("/var/www"); // p1.getNameCount() returns 2
  • getName(int index) 返回给定索引处的元素

     placeholderCopyPath p1 = Paths.get("/var/www"); // p1.getName(0) returns "var", p1.getName(1) returns "www"
  • getParent() 返回父目录的路径

     placeholderCopyPath p1 = Paths.get("/var/www"); // p1.getParent().toString() returns "/var"
  • getRoot() 返回路径的根

     placeholderCopyPath p1 = Paths.get("/var/www"); // p1.getRoot().toString() returns "/"
    Path p3 = Paths.get("C:\\Users\\DentAr\\Documents\\HHGTDG.odt"); // p3.getRoot().toString() returns "C:\\"