// files/PathInfo.java// (c)2017 MindView LLC: see Copyright.txt// We make no guarantees that this code is fit for any purpose.// Visit http://OnJava8.com for more book information.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}}/* Output:Windows 10toString: C:\path\to\nowhere\NoFile.txtExists: falseRegularFile: falseDirectory: falseAbsolute: trueFileName: NoFile.txtParent: C:\path\to\nowhereRoot: C:\******************toString: PathInfo.javaExists: trueRegularFile: trueDirectory: falseAbsolute: falseFileName: PathInfo.javaParent: nullRoot: null******************toString: C:\Users\Bruce\Documents\GitHub\on-java\ExtractedExamples\files\PathInfo.javaExists: trueRegularFile: trueDirectory: falseAbsolute: trueFileName: PathInfo.javaParent: C:\Users\Bruce\Documents\GitHub\on-java\ExtractedExamples\filesRoot: C:\******************toString: C:\Users\Bruce\Documents\GitHub\on-java\ExtractedExamples\filesExists: trueRegularFile: falseDirectory: trueAbsolute: trueFileName: filesParent: C:\Users\Bruce\Documents\GitHub\on-java\ExtractedExamplesRoot: C:\******************toString: C:\Users\Bruce\Documents\GitHub\on-java\ExtractedExamples\files\PathInfo.javaExists: trueRegularFile: trueDirectory: falseAbsolute: trueFileName: PathInfo.javaParent: C:\Users\Bruce\Documents\GitHub\on-java\ExtractedExamples\filesRoot: C:\******************URI: file:///C:/Users/Bruce/Documents/GitHub/on-java/ExtractedExamples/files/PathInfo.javatrue*/
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。