2015-12-02 09:20:27 -08:00
|
|
|
// files/AddAndSubtractPaths.java
|
2015-12-15 11:47:04 -08:00
|
|
|
// (c)2016 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.*;
|
2015-12-06 11:45:16 -08:00
|
|
|
import java.io.IOException;
|
2015-12-02 09:20:27 -08:00
|
|
|
|
|
|
|
public class AddAndSubtractPaths {
|
|
|
|
static Path base = Paths.get("..", "..", "..")
|
|
|
|
.toAbsolutePath()
|
|
|
|
.normalize();
|
|
|
|
static void show(int id, Path result) {
|
|
|
|
if(result.isAbsolute())
|
|
|
|
System.out.println("(" + id + ")r " +
|
|
|
|
base.relativize(result));
|
|
|
|
else
|
|
|
|
System.out.println("(" + id + ") " + result);
|
2015-12-06 11:45:16 -08:00
|
|
|
try {
|
2016-01-25 18:05:55 -08:00
|
|
|
System.out.println("RealPath: "
|
|
|
|
+ result.toRealPath());
|
2015-12-06 11:45:16 -08:00
|
|
|
} catch (IOException e) {
|
|
|
|
System.out.println(e);
|
|
|
|
}
|
2015-12-02 09:20:27 -08:00
|
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
|
|
System.out.println(System.getProperty("os.name"));
|
|
|
|
System.out.println(base);
|
|
|
|
Path p = Paths.get("AddAndSubtractPaths.java")
|
|
|
|
.toAbsolutePath();
|
|
|
|
show(1, p);
|
|
|
|
Path convoluted = p.getParent().getParent()
|
|
|
|
.resolve("strings")
|
|
|
|
.resolve("..")
|
|
|
|
.resolve(p.getParent().getFileName());
|
|
|
|
show(2, convoluted);
|
|
|
|
show(3, convoluted.normalize());
|
|
|
|
|
|
|
|
Path p2 = Paths.get("..", "..");
|
|
|
|
show(4, p2);
|
|
|
|
show(5, p2.normalize());
|
|
|
|
show(6, p2.toAbsolutePath().normalize());
|
|
|
|
|
|
|
|
Path p3 = Paths.get(".").toAbsolutePath();
|
|
|
|
Path p4 = p3.resolve(p2);
|
|
|
|
show(7, p4);
|
|
|
|
show(8, p4.normalize());
|
|
|
|
|
|
|
|
Path p5 = Paths.get("").toAbsolutePath();
|
|
|
|
show(9, p5);
|
|
|
|
show(10, p5.resolveSibling("strings"));
|
2015-12-06 11:45:16 -08:00
|
|
|
show(11, Paths.get("nonexistent"));
|
2015-12-02 09:20:27 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Output:
|
|
|
|
Windows 10
|
2016-07-22 14:45:35 -06:00
|
|
|
C:\Users\Bruce\Documents\GitHub
|
2015-12-02 09:20:27 -08:00
|
|
|
(1)r on-
|
|
|
|
java\ExtractedExamples\files\AddAndSubtractPaths.java
|
2016-07-22 14:45:35 -06:00
|
|
|
RealPath: C:\Users\Bruce\Documents\GitHub\on-
|
2015-12-06 11:45:16 -08:00
|
|
|
java\ExtractedExamples\files\AddAndSubtractPaths.java
|
2015-12-02 09:20:27 -08:00
|
|
|
(2)r on-java\ExtractedExamples\strings\..\files
|
2016-07-22 14:45:35 -06:00
|
|
|
RealPath: C:\Users\Bruce\Documents\GitHub\on-
|
2015-12-06 11:45:16 -08:00
|
|
|
java\ExtractedExamples\files
|
2015-12-02 09:20:27 -08:00
|
|
|
(3)r on-java\ExtractedExamples\files
|
2016-07-22 14:45:35 -06:00
|
|
|
RealPath: C:\Users\Bruce\Documents\GitHub\on-
|
2015-12-06 11:45:16 -08:00
|
|
|
java\ExtractedExamples\files
|
2015-12-02 09:20:27 -08:00
|
|
|
(4) ..\..
|
2016-07-22 14:45:35 -06:00
|
|
|
RealPath: C:\Users\Bruce\Documents\GitHub\on-java
|
2015-12-02 09:20:27 -08:00
|
|
|
(5) ..\..
|
2016-07-22 14:45:35 -06:00
|
|
|
RealPath: C:\Users\Bruce\Documents\GitHub\on-java
|
2015-12-02 09:20:27 -08:00
|
|
|
(6)r on-java
|
2016-07-22 14:45:35 -06:00
|
|
|
RealPath: C:\Users\Bruce\Documents\GitHub\on-java
|
2015-12-02 09:20:27 -08:00
|
|
|
(7)r on-java\ExtractedExamples\files\.\..\..
|
2016-07-22 14:45:35 -06:00
|
|
|
RealPath: C:\Users\Bruce\Documents\GitHub\on-java
|
2015-12-02 09:20:27 -08:00
|
|
|
(8)r on-java
|
2016-07-22 14:45:35 -06:00
|
|
|
RealPath: C:\Users\Bruce\Documents\GitHub\on-java
|
2015-12-02 09:20:27 -08:00
|
|
|
(9)r on-java\ExtractedExamples\files
|
2016-07-22 14:45:35 -06:00
|
|
|
RealPath: C:\Users\Bruce\Documents\GitHub\on-
|
2015-12-06 11:45:16 -08:00
|
|
|
java\ExtractedExamples\files
|
2015-12-02 09:20:27 -08:00
|
|
|
(10)r on-java\ExtractedExamples\strings
|
2016-07-22 14:45:35 -06:00
|
|
|
RealPath: C:\Users\Bruce\Documents\GitHub\on-
|
2015-12-06 11:45:16 -08:00
|
|
|
java\ExtractedExamples\strings
|
|
|
|
(11) nonexistent
|
|
|
|
java.nio.file.NoSuchFileException:
|
2016-07-22 14:45:35 -06:00
|
|
|
C:\Users\Bruce\Documents\GitHub\on-
|
2015-12-06 11:45:16 -08:00
|
|
|
java\ExtractedExamples\files\nonexistent
|
2015-12-02 09:20:27 -08:00
|
|
|
*/
|