🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## [文件和目录路径](https://lingcoder.gitee.io/onjava8/#/book/17-Files?id=%e6%96%87%e4%bb%b6%e5%92%8c%e7%9b%ae%e5%bd%95%e8%b7%af%e5%be%84) 一个**Path**对象表示一个文件或者目录的路径,是一个跨操作系统(OS)和文件系统的抽象,目的是在构造路径时不必关注底层操作系统,代码可以在不进行修改的情况下运行在不同的操作系统上。**java.nio.file.Paths**类包含一个重载方法**static get()\*\*,该方法接受一系列 \*\*String**字符串或一个*统一资源标识符*(URI)作为参数,并且进行转换返回一个**Path**对象: ~~~ // files/PathInfo.java import java.nio.file.*; import java.net.URI; import java.io.File; import java.io.IOException; public class PathInfo { static void show(String id, Object p) { System.out.println(id + ": " + p); } static void info(Path p) { show("toString", p); show("Exists", Files.exists(p)); show("RegularFile", Files.isRegularFile(p)); show("Directory", Files.isDirectory(p)); show("Absolute", p.isAbsolute()); show("FileName", p.getFileName()); show("Parent", p.getParent()); show("Root", p.getRoot()); System.out.println("******************"); } public static void main(String[] args) { System.out.println(System.getProperty("os.name")); info(Paths.get("C:", "path", "to", "nowhere", "NoFile.txt")); Path p = Paths.get("PathInfo.java"); info(p); Path ap = p.toAbsolutePath(); info(ap); info(ap.getParent()); try { info(p.toRealPath()); } catch(IOException e) { System.out.println(e); } URI u = p.toUri(); System.out.println("URI: " + u); Path puri = Paths.get(u); System.out.println(Files.exists(puri)); File f = ap.toFile(); // Don't be fooled } } /* 输出: Windows 10 toString: C:\path\to\nowhere\NoFile.txt Exists: false RegularFile: false Directory: false Absolute: true FileName: NoFile.txt Parent: C:\path\to\nowhere Root: C:\ ****************** toString: PathInfo.java Exists: true RegularFile: true Directory: false Absolute: false FileName: PathInfo.java Parent: null Root: null ****************** toString: C:\Users\Bruce\Documents\GitHub\onjava\ ExtractedExamples\files\PathInfo.java Exists: true RegularFile: true Directory: false Absolute: true FileName: PathInfo.java Parent: C:\Users\Bruce\Documents\GitHub\onjava\ ExtractedExamples\files Root: C:\ ****************** toString: C:\Users\Bruce\Documents\GitHub\onjava\ ExtractedExamples\files Exists: true RegularFile: false Directory: true Absolute: true FileName: files Parent: C:\Users\Bruce\Documents\GitHub\onjava\ ExtractedExamples Root: C:\ ****************** toString: C:\Users\Bruce\Documents\GitHub\onjava\ ExtractedExamples\files\PathInfo.java Exists: true RegularFile: true Directory: false Absolute: true FileName: PathInfo.java Parent: C:\Users\Bruce\Documents\GitHub\onjava\ ExtractedExamples\files Root: C:\ ****************** URI: file:///C:/Users/Bruce/Documents/GitHub/onjava/ ExtractedExamples/files/PathInfo.java true */ ~~~ 我已经在这一章第一个程序的**main()**方法添加了第一行用于展示操作系统的名称,因此你可以看到不同操作系统之间存在哪些差异。理想情况下,差别会相对较小,并且使用**/**或者**\\**路径分隔符进行分隔。你可以看到我运行在Windows 10 上的程序输出。 当**toString()**方法生成完整形式的路径,你可以看到**getFileName()**方法总是返回当前文件名。 通过使用**Files**工具类(我们接下来将会更多地使用它),可以测试一个文件是否存在,测试是否是一个"普通"文件还是一个目录等等。"Nofile.txt"这个示例展示我们描述的文件可能并不在指定的位置;这样可以允许你创建一个新的路径。"PathInfo.java"存在于当前目录中,最初它只是没有路径的文件名,但它仍然被检测为"存在"。一旦我们将其转换为绝对路径,我们将会得到一个从"C:"盘(因为我们是在Windows机器下进行测试)开始的完整路径,现在它也拥有一个父路径。“真实”路径的定义在文档中有点模糊,因为它取决于具体的文件系统。例如,如果文件名不区分大小写,即使路径由于大小写的缘故而不是完全相同,也可能得到肯定的匹配结果。在这样的平台上,**toRealPath()**将返回实际情况下的**Path**,并且还会删除任何冗余元素。 这里你会看到**URI**看起来只能用于描述文件,实际上**URI**可以用于描述更多的东西;通过[维基百科](https://en.wikipedia.org/wiki/Uniform_Resource_Identifier)可以了解更多细节。现在我们成功地将**URI**转为一个**Path**对象。 最后,你会在**Path**中看到一些有点欺骗的东西,这就是调用**toFile()**方法会生成一个**File**对象。听起来似乎可以得到一个类似文件的东西(毕竟被称为**File**),但是这个方法的存在仅仅是为了向后兼容。虽然看上去应该被称为"路径",实际上却应该表示目录或者文件本身。这是个非常草率并且令人困惑的命名,但是由于**java.nio.file**的存在我们可以安全地忽略它的存在。