|
| 1 | +// files/AddAndSubtractPaths.java |
| 2 | +// (c)2020 MindView LLC: see Copyright.txt |
| 3 | +// We make no guarantees that this code is fit for any purpose. |
| 4 | +// Visit http://OnJava8.com for more book information. |
| 5 | +import java.nio.file.*; |
| 6 | +import java.io.IOException; |
| 7 | + |
| 8 | +public class AddAndSubtractPaths { |
| 9 | + static Path base = Paths.get("..", "..", "..") |
| 10 | + .toAbsolutePath() |
| 11 | + .normalize(); |
| 12 | + static void show(int id, Path result) { |
| 13 | + if(result.isAbsolute()) |
| 14 | + System.err.println("(" + id + ")[relativize] " + |
| 15 | + base.relativize(result)); |
| 16 | + else |
| 17 | + System.err.println("(" + id + ") " + result); |
| 18 | + try { |
| 19 | + System.err.println("RealPath: " |
| 20 | + + result.toRealPath()); |
| 21 | + } catch(IOException e) { |
| 22 | + System.err.println(e); |
| 23 | + } |
| 24 | + } |
| 25 | + public static void main(String[] args) { |
| 26 | + System.err.println(System.getProperty("os.name")); |
| 27 | + System.err.println(base); |
| 28 | + Path p = Paths.get("AddAndSubtractPaths.java") |
| 29 | + .toAbsolutePath(); |
| 30 | + show(1, p); |
| 31 | + Path convoluted = p.getParent().getParent() |
| 32 | + .resolve("strings") |
| 33 | + .resolve("..") |
| 34 | + .resolve(p.getParent().getFileName()); |
| 35 | + show(2, convoluted); |
| 36 | + show(3, convoluted.normalize()); |
| 37 | + |
| 38 | + Path p2 = Paths.get("..", ".."); |
| 39 | + show(4, p2); |
| 40 | + show(5, p2.normalize()); |
| 41 | + show(6, p2.toAbsolutePath().normalize()); |
| 42 | + |
| 43 | + Path p3 = Paths.get(".").toAbsolutePath(); |
| 44 | + Path p4 = p3.resolve(p2); |
| 45 | + show(7, p4); |
| 46 | + show(8, p4.normalize()); |
| 47 | + |
| 48 | + Path p5 = Paths.get("").toAbsolutePath(); |
| 49 | + show(9, p5); |
| 50 | + show(10, p5.resolveSibling("strings")); |
| 51 | + show(11, Paths.get("nonexistent")); |
| 52 | + } |
| 53 | +} |
| 54 | +/* Output: |
| 55 | +Windows 10 |
| 56 | +C:\Users\Bruce\Documents\GitHub |
| 57 | +(1)r on- |
| 58 | +java\ExtractedExamples\files\AddAndSubtractPaths.java |
| 59 | +RealPath: C:\Users\Bruce\Documents\GitHub\on- |
| 60 | +java\ExtractedExamples\files\AddAndSubtractPaths.java |
| 61 | +(2)r on-java\ExtractedExamples\strings\..\files |
| 62 | +RealPath: C:\Users\Bruce\Documents\GitHub\on- |
| 63 | +java\ExtractedExamples\files |
| 64 | +(3)r on-java\ExtractedExamples\files |
| 65 | +RealPath: C:\Users\Bruce\Documents\GitHub\on- |
| 66 | +java\ExtractedExamples\files |
| 67 | +(4) ..\.. |
| 68 | +RealPath: C:\Users\Bruce\Documents\GitHub\on-java |
| 69 | +(5) ..\.. |
| 70 | +RealPath: C:\Users\Bruce\Documents\GitHub\on-java |
| 71 | +(6)r on-java |
| 72 | +RealPath: C:\Users\Bruce\Documents\GitHub\on-java |
| 73 | +(7)r on-java\ExtractedExamples\files\.\..\.. |
| 74 | +RealPath: C:\Users\Bruce\Documents\GitHub\on-java |
| 75 | +(8)r on-java |
| 76 | +RealPath: C:\Users\Bruce\Documents\GitHub\on-java |
| 77 | +(9)r on-java\ExtractedExamples\files |
| 78 | +RealPath: C:\Users\Bruce\Documents\GitHub\on- |
| 79 | +java\ExtractedExamples\files |
| 80 | +(10)r on-java\ExtractedExamples\strings |
| 81 | +RealPath: C:\Users\Bruce\Documents\GitHub\on- |
| 82 | +java\ExtractedExamples\strings |
| 83 | +(11) nonexistent |
| 84 | +java.nio.file.NoSuchFileException: |
| 85 | +C:\Users\Bruce\Documents\GitHub\on- |
| 86 | +java\ExtractedExamples\files\nonexistent |
| 87 | +*/ |
0 commit comments