2015-12-02 09:20:27 -08:00
|
|
|
// files/PathInfo.java
|
2020-10-07 13:35:40 -06:00
|
|
|
// (c)2020 MindView LLC: see Copyright.txt
|
2015-12-02 09:20:27 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2015-12-02 09:20:27 -08:00
|
|
|
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());
|
2017-05-10 11:45:39 -06:00
|
|
|
System.out.println("******************");
|
2015-12-02 09:20:27 -08:00
|
|
|
}
|
|
|
|
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());
|
2017-01-22 16:48:11 -08:00
|
|
|
} catch(IOException e) {
|
2015-12-02 09:20:27 -08:00
|
|
|
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 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:\
|
2017-05-10 11:45:39 -06:00
|
|
|
******************
|
2015-12-02 09:20:27 -08:00
|
|
|
toString: PathInfo.java
|
|
|
|
Exists: true
|
|
|
|
RegularFile: true
|
|
|
|
Directory: false
|
|
|
|
Absolute: false
|
|
|
|
FileName: PathInfo.java
|
|
|
|
Parent: null
|
|
|
|
Root: null
|
2017-05-10 11:45:39 -06:00
|
|
|
******************
|
|
|
|
toString: C:\Users\Bruce\Documents\GitHub\on-
|
2015-12-02 09:20:27 -08:00
|
|
|
java\ExtractedExamples\files\PathInfo.java
|
|
|
|
Exists: true
|
|
|
|
RegularFile: true
|
|
|
|
Directory: false
|
|
|
|
Absolute: true
|
|
|
|
FileName: PathInfo.java
|
2017-05-10 11:45:39 -06:00
|
|
|
Parent: C:\Users\Bruce\Documents\GitHub\on-
|
2015-12-02 09:20:27 -08:00
|
|
|
java\ExtractedExamples\files
|
|
|
|
Root: C:\
|
2017-05-10 11:45:39 -06:00
|
|
|
******************
|
|
|
|
toString: C:\Users\Bruce\Documents\GitHub\on-
|
2015-12-02 09:20:27 -08:00
|
|
|
java\ExtractedExamples\files
|
|
|
|
Exists: true
|
|
|
|
RegularFile: false
|
|
|
|
Directory: true
|
|
|
|
Absolute: true
|
|
|
|
FileName: files
|
2017-05-10 11:45:39 -06:00
|
|
|
Parent: C:\Users\Bruce\Documents\GitHub\on-
|
2015-12-02 09:20:27 -08:00
|
|
|
java\ExtractedExamples
|
|
|
|
Root: C:\
|
2017-05-10 11:45:39 -06:00
|
|
|
******************
|
|
|
|
toString: C:\Users\Bruce\Documents\GitHub\on-
|
2015-12-02 09:20:27 -08:00
|
|
|
java\ExtractedExamples\files\PathInfo.java
|
|
|
|
Exists: true
|
|
|
|
RegularFile: true
|
|
|
|
Directory: false
|
|
|
|
Absolute: true
|
|
|
|
FileName: PathInfo.java
|
2017-05-10 11:45:39 -06:00
|
|
|
Parent: C:\Users\Bruce\Documents\GitHub\on-
|
2015-12-02 09:20:27 -08:00
|
|
|
java\ExtractedExamples\files
|
|
|
|
Root: C:\
|
2017-05-10 11:45:39 -06:00
|
|
|
******************
|
|
|
|
URI: file:///C:/Users/Bruce/Documents/GitHub/on-
|
2015-12-02 09:20:27 -08:00
|
|
|
java/ExtractedExamples/files/PathInfo.java
|
|
|
|
true
|
|
|
|
*/
|