Java - Demonstrate method from Path

Description

Demonstrate method from Path

Demo

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;

public class PathHelper {

  public static void main(String[] args) throws IOException {

    System.out.println("Method prototype:          int\tcompareTo(Path other)\n"
        + "Purpose:                   Compares two abstract paths lexicographically.\n");
    Path path1 = Paths.get("/tmp/foo");
    Path path1a = Paths.get("/tmp/foo");
    Path path1b = Paths.get("/tmp/bar");
    Path path1c = Paths.get("/tmp/./bar");
    Path path1d = Paths.get("/tmp/../tmp/bar/foo/..");
    Path path1e = Paths.get("/tmp/foo/");
    System.out.println("Comparing /tmp/foo and /tmp/bar: " + path1.compareTo(path1b));
    System.out.println("Comparing /tmp/foo and /tmp/foo (same object): " + path1.compareTo(path1));
    System.out.println("Comparing /tmp/foo and /tmp/foo (different object): " + path1.compareTo(path1a));
    System.out.println("Comparing /tmp/bar and /tmp/./bar: " + path1b.compareTo(path1c));
    System.out.println("Comparing /tmp/bar and /tmp/../tmp/bar/foo/..: " + path1b.compareTo(path1d));
    System.out.println("Comparing /tmp/foo and /tmp/foo/: " + path1b.compareTo(path1e));

    System.out.println("Method prototype:          boolean\tendsWith(Path other)\n"
        + "Purpose:                   Tests if this path ends with the given path.\n");
    Path path2 = Paths.get("/tmp/foo");
    Path path2b = Paths.get("foo");
    Path path2b1 = Paths.get("foo/");
    Path path2b2 = Paths.get("foo/bar/baz/quz/../../..");
    Path path2b3 = Paths.get("foo/.");
    Path path2b4 = Paths.get("foo/../foo");
    Path path2c = Paths.get("/foo");
    Path path2d = Paths.get("tmp/foo");
    Path path2e = Paths.get("/tmp/foo/");
    System.out.println("/tmp/foo endsWith foo: " + path2.endsWith(path2b));
    System.out.println("/tmp/foo endsWith foo/bar/baz/quz/../../..: " + path2.endsWith(path2b2));
    System.out.println("/tmp/foo endsWith foo/.: " + path2.endsWith(path2b3));
    System.out.println("/tmp/foo endsWith foo/../foo: " + path2.endsWith(path2b4));
    System.out.println("/tmp/foo endsWith /foo: " + path2.endsWith(path2c));
    System.out.println("/tmp/foo endsWith tmp/foo: " + path2.endsWith(path2d));
    System.out.println("/tmp/foo endsWith /tmp/foo/: " + path2.endsWith(path2e));
    System.out.println("CAVEAT: lexicographical operation, not smart about different ways to get to the same path");

    System.out.println("Method prototype:          boolean\tendsWith(String other)\n"
        + "Purpose:                   Tests if this path ends with a Path, constructed by converting the "
        + "given path string, in exactly the manner specified by the endsWith(Path) method.\n");
    System.out.println("/tmp/foo endsWith foo: " + path2.endsWith("foo"));
    System.out.println("/tmp/foo endsWith foo/bar/baz/quz/../../..: " + path2.endsWith("foo/bar/baz/quz/../../.."));
    System.out.println("/tmp/foo endsWith foo/.: " + path2.endsWith("foo/."));
    System.out.println("/tmp/foo endsWith foo/../foo: " + path2.endsWith("foo/../foo"));
    System.out.println("/tmp/foo endsWith /foo: " + path2.endsWith("/foo"));
    System.out.println("/tmp/foo endsWith tmp/foo: " + path2.endsWith("tmp/foo"));
    System.out.println("/tmp/foo endsWith /tmp/foo/: " + path2.endsWith("/tmp/foo/"));
    System.out.println("CAVEAT: lexicographical operation, not smart about different ways to get to the same path.");

    System.out.println("Method prototype:          boolean\tequals(Object other)\n"
        + "Purpose:                   Tests this path for equality with the given object.\n");
    System.out.println("/tmp/foo equals foo: " + path2.equals(path2b));
    System.out.println("/tmp/foo equals /tmp/foo (different object): " + path2.equals(path1a));
    System.out.println("/tmp/foo equals foo/bar/baz/quz/../../..: " + path2.equals(path2b2));
    System.out.println("/tmp/foo equals foo/.: " + path2.equals(path2b3));
    System.out.println("/tmp/foo equals foo/../foo: " + path2.equals(path2b4));
    System.out.println("/tmp/foo equals /foo: " + path2.equals(path2c));
    System.out.println("/tmp/foo equals tmp/foo: " + path2.equals(path2d));
    System.out.println("CAVEAT: lexicographical operation, not smart about different ways to get to the same path.");

    System.out.println("Method prototype:          Path\tgetFileName()\n"
        + "Purpose:                   Returns the name of the file or directory denoted by this path as"
        + " a Path object.\n");
    System.out.println("/tmp/foo getFileName: " + path2.getFileName());
    System.out.println("foo getFileName: " + path2b.getFileName());
    System.out.println("foo/bar/baz/quz/../../.. getFileName: " + path2b2.getFileName());
    System.out.println("foo/. getFileName: " + path2b3.getFileName());
    System.out.println("foo/../foo getFileName: " + path2b4.getFileName());
    System.out.println("/foo getFileName: " + path2c.getFileName());
    System.out.println("tmp/foo getFileName: " + path2d.getFileName());
    System.out.println("tmp/foo/ getFileName: " + path2e.getFileName());
    System.out.println("CAVEAT: Ending slash will be ignored, but not ending . or ..");

    System.out.println("Method prototype:          FileSystem\tgetFileSystem()\n"
        + "Purpose:                   Returns the file system that created this object.\n");
    System.out.println("/tmp/foo getFileSystem: " + path2.getFileSystem());
    Path pathWin = Paths.get("C:\\tmp");
    System.out.println("C:\\tmp getFileSystem: " + pathWin.getFileSystem());
    System.out.println("CAVEAT: don't care what you pass, it will return the physical underlying file system of the "
        + "machine where it's run");

    System.out.println("Method prototype:          Path\tgetName(int index)\n"
        + "Purpose:                   Returns a name element of this path as a Path object.\n");
    System.out.println("/tmp/foo getName 0: " + path2.getName(0));
    System.out.println("/tmp/foo getName 1: " + path2.getName(1));
    // System.out.println("/tmp/foo getName 2: " + path2.getName(2));
    System.out.println("foo getName 0: " + path2b.getName(0));
    // System.out.println("foo getName 1: " + path2b.getName(1));
    System.out.println("foo/bar/baz/quz/../../.. getName 0: " + path2b2.getName(0));
    System.out.println("foo/bar/baz/quz/../../.. getName 1: " + path2b2.getName(1));
    System.out.println("foo/bar/baz/quz/../../.. getName 2: " + path2b2.getName(2));
    System.out.println("foo/bar/baz/quz/../../.. getName 3: " + path2b2.getName(3));
    System.out.println("foo/bar/baz/quz/../../.. getName 4: " + path2b2.getName(4));
    System.out.println("foo/bar/baz/quz/../../.. getName 5: " + path2b2.getName(5));
    System.out.println("foo/bar/baz/quz/../../.. getName 6: " + path2b2.getName(6));
    // System.out.println("foo/bar/baz/quz/../../.. getName 7: " +
    // path2b2.getName(7));
    System.out.println("foo/. getName 0: " + path2b3.getName(0));
    System.out.println("foo/. getName 1: " + path2b3.getName(1));
    System.out.println("foo/../foo getName: " + path2b4.getName(0));
    System.out.println("foo/../foo getName: " + path2b4.getName(1));
    System.out.println("foo/../foo getName: " + path2b4.getName(2));
    System.out.println("/foo getName 0: " + path2c.getName(0));
    // System.out.println("/foo getName 1: " + path2c.getName(1));
    System.out.println("tmp/foo getName 0: " + path2d.getName(0));
    System.out.println("tmp/foo getName 1: " + path2d.getName(1));
    System.out.println("tmp/foo/ getName 0: " + path2e.getName(0));
    System.out.println("tmp/foo/ getName 1: " + path2e.getName(1));
    // System.out.println("tmp/foo/ getName 2: " + path2e.getName(2));
    System.out.println("CAVEAT: Ending slash will be ignored, but not ending . or ..");

    System.out.println("Method prototype:          int\tgetNameCount()\n"
        + "Purpose:                   Returns the number of name elements in the path.\n");
    System.out.println("/tmp/foo getNameCount: " + path2.getNameCount());
    System.out.println("foo/bar/baz/quz/../../.. getNameCount: " + path2b2.getNameCount());
    System.out.println("foo/. getNameCount: " + path2b3.getNameCount());
    System.out.println("foo/../foo getNameCount: " + path2b4.getNameCount());
    System.out.println("/foo getNameCount: " + path2c.getNameCount());
    System.out.println("tmp/foo getNameCount: " + path2d.getNameCount());
    System.out.println("/tmp/foo/ getNameCount: " + path2e.getNameCount());
    System.out.println("CAVEAT: lexicographical operation, not smart about different ways to get to the same path");

    System.out.println("Method prototype:          Path\tgetParent()\n"
        + "Purpose:                   Returns the parent path, or null if this path does not have a parent.\n");
    Path pathRoot = Paths.get("/");
    Path pathRootCurrent = Paths.get("/.");
    Path pathEmpty = Paths.get("");
    Path pathEmptyCurrent = Paths.get(".");
    System.out.println("/tmp/foo getParent: " + path2.getParent());
    System.out.println("foo/bar/baz/quz/../../.. getParent: " + path2b2.getParent());
    System.out.println("foo/. getParent: " + path2b3.getParent());
    System.out.println("foo/../foo getParent: " + path2b4.getParent());
    System.out.println("/foo getParent: " + path2c.getParent());
    System.out.println("tmp/foo getParent: " + path2d.getParent());
    System.out.println("/tmp/foo/ getParent: " + path2e.getParent());
    System.out.println("/ getParent: " + pathRoot.getParent());
    System.out.println("/. getParent: " + pathRootCurrent.getParent());
    System.out.println("(empty) getParent: " + pathEmpty.getParent());
    System.out.println(". getParent: " + pathEmptyCurrent.getParent());
    System.out.println("CAVEAT: careful with the current directory, if it has a parent, it'll return it, even if root");

    System.out.println("Method prototype:          Path\tgetRoot()\n"
        + "Purpose:                   Returns the root component of this path as a Path object, or "
        + "null if this path does not have a root component.\n");
    System.out.println("/tmp/foo getRoot: " + path2.getRoot());
    System.out.println("foo/bar/baz/quz/../../.. getRoot: " + path2b2.getRoot());
    System.out.println("foo/. getRoot: " + path2b3.getRoot());
    System.out.println("foo/../foo getRoot: " + path2b4.getRoot());
    System.out.println("/foo getRoot: " + path2c.getRoot());
    System.out.println("tmp/foo getRoot: " + path2d.getRoot());
    System.out.println("/tmp/foo/ getRoot: " + path2e.getRoot());
    System.out.println("/ getRoot: " + pathRoot.getRoot());
    System.out.println("/. getRoot: " + pathRootCurrent.getRoot());
    System.out.println("(empty) getRoot: " + pathEmpty.getRoot());
    System.out.println(". getRoot: " + pathEmptyCurrent.getRoot());
    System.out.println("C:\\tmp getRoot: " + pathWin.getRoot());
    System.out.println("CAVEAT: careful with the windows path if not in a windows system!");

    System.out.println("Method prototype:          boolean\tisAbsolute()\n"
        + "Purpose:                   Tells whether or not this path is absolute.\n");
    System.out.println("/tmp/foo isAbsolute: " + path2.isAbsolute());
    System.out.println("foo/bar/baz/quz/../../.. isAbsolute: " + path2b2.isAbsolute());
    System.out.println("foo/. isAbsolute: " + path2b3.isAbsolute());
    System.out.println("foo/../foo isAbsolute: " + path2b4.isAbsolute());
    System.out.println("/foo isAbsolute: " + path2c.isAbsolute());
    System.out.println("tmp/foo isAbsolute: " + path2d.isAbsolute());
    System.out.println("/tmp/foo/ isAbsolute: " + path2e.isAbsolute());
    System.out.println("/ isAbsolute: " + pathRoot.isAbsolute());
    System.out.println("/. isAbsolute: " + pathRootCurrent.isAbsolute());
    System.out.println("(empty) isAbsolute: " + pathEmpty.isAbsolute());
    System.out.println(". isAbsolute: " + pathEmptyCurrent.isAbsolute());
    System.out.println("C:\\tmp isAbsolute: " + pathWin.isAbsolute());
    System.out.println("CAVEAT: careful with the windows path if not in a windows system!");

    System.out.println("Method prototype:          Path\tnormalize()\n"
        + "Purpose:                   Returns a path that is this path with redundant name elements eliminated.\n");
    System.out.println("/tmp/foo normalize: " + path2.normalize());
    System.out.println("foo/bar/baz/quz/../../.. normalize: " + path2b2.normalize());
    System.out.println("foo/. normalize: " + path2b3.normalize());
    System.out.println("foo/../foo normalize: " + path2b4.normalize());
    System.out.println("/foo normalize: " + path2c.normalize());
    System.out.println("tmp/foo normalize: " + path2d.normalize());
    System.out.println("/tmp/foo/ normalize: " + path2e.normalize());
    System.out.println("/ normalize: " + pathRoot.normalize());
    System.out.println("/. normalize: " + pathRootCurrent.normalize());
    // System.out.println("(empty) normalize: " + pathEmpty.normalize());
    System.out.println(". normalize: " + pathEmptyCurrent.normalize());
    System.out.println("C:\\tmp normalize: " + pathWin.normalize());
    System.out.println("CAVEAT: it will remove the current directory as well from the output");

    System.out.println("Method prototype:          Path\ttoAbsolutePath()\n"
        + "Purpose:                   Returns a Path object representing the absolute path of this path.\n");

    System.out.println("/tmp/foo toAbsolutePath: " + path2.toAbsolutePath());
    System.out.println("foo/bar/baz/quz/../../.. toAbsolutePath: " + path2b2.toAbsolutePath());
    System.out.println("foo/. toAbsolutePath: " + path2b3.toAbsolutePath());
    System.out.println("foo/../foo toAbsolutePath: " + path2b4.toAbsolutePath());
    System.out.println("/foo toAbsolutePath: " + path2c.toAbsolutePath());
    System.out.println("tmp/foo toAbsolutePath: " + path2d.toAbsolutePath());
    System.out.println("/tmp/foo/ toAbsolutePath: " + path2e.toAbsolutePath());
    System.out.println("/ toAbsolutePath: " + pathRoot.toAbsolutePath());
    System.out.println("/. toAbsolutePath: " + pathRootCurrent.toAbsolutePath());
    System.out.println("(empty) toAbsolutePath: " + pathEmpty.toAbsolutePath());
    System.out.println(". toAbsolutePath: " + pathEmptyCurrent.toAbsolutePath());
    System.out.println("C:\\tmp toAbsolutePath: " + pathWin.toAbsolutePath());
    System.out.println("CAVEAT: careful with the windows path if not in a windows system, not a root here!");
    System.out.println("CAVEAT: it does NOT remove the current directory, nor the changes ..");

    System.out.println("Method prototype:          File\ttoFile()\n"
        + "Purpose:                   Returns a File object representing this path.\n");
    System.out.println("/tmp/foo toFile: " + path2.toFile());
    System.out.println("foo/bar/baz/quz/../../.. toFile: " + path2b2.toFile());
    System.out.println("foo/. toFile: " + path2b3.toFile());
    System.out.println("foo/../foo toFile: " + path2b4.toFile());
    System.out.println("/foo toFile: " + path2c.toFile());
    System.out.println("tmp/foo toFile: " + path2d.toFile());
    System.out.println("/tmp/foo/ toFile: " + path2e.toFile());
    System.out.println("/ toFile: " + pathRoot.toFile());
    System.out.println("/. toFile: " + pathRootCurrent.toFile());
    System.out.println("(empty) toFile: " + pathEmpty.toFile());
    System.out.println(". toFile: " + pathEmptyCurrent.toFile());
    System.out.println("C:\\tmp toFile: " + pathWin.toFile());
    System.out.println("CAVEAT: File toString is exactly the same as Path toString... all you get from here");

    System.out.println("Method prototype:          Path toRealPath(LinkOption... options)\n"
        + "Purpose:                   Returns the real path of an existing file.\n");

    System.out.println("CAVEAT: if the file does not exist, it throws java.nio.file.NoSuchFileException!");

    System.out.println("Method prototype:          Path\trelativize(Path other)\n"
        + "Purpose:                   Constructs a relative path between this path and a given path.\n");

    System.out.println(// w w w . j a  va 2  s  .  c o m
        "/tmp/foo relativize foo/bar/baz/quz/../../..: java.lang.IllegalArgumentException: 'other' is different type of Path");
    System.out.println("foo/bar/baz/quz/../../.. relativize foo/.: " + path2b2.relativize(path2b3));
    System.out.println("foo/. relativize foo/../foo: " + path2b3.relativize(path2b4));
    System.out
        .println("foo/../foo relativize /foo: java.lang.IllegalArgumentException: 'other' is different type of Path");
    System.out
        .println("/foo relativize tmp/foo: java.lang.IllegalArgumentException: 'other' is different type of Path");
    System.out
        .println("tmp/foo relativize /tmp/foo/: java.lang.IllegalArgumentException: 'other' is different type of Path");
    System.out.println("/tmp/foo/ relativize /: " + path2e.relativize(pathRoot));
    System.out.println("/ relativize /.: " + pathRoot.relativize(pathRootCurrent));
    System.out.println("/. relativize (empty): java.lang.IllegalArgumentException: 'other' is different type of Path");
    System.out.println("(empty) relativize .: " + pathEmpty.relativize(pathEmptyCurrent));
    System.out.println(". relativize C:\\tmp: " + pathEmptyCurrent.relativize(pathWin));
    System.out.println("C:\\tmp relativize C:\\tmp: " + pathWin.relativize(pathWin));
    System.out
        .println("/./././. relativize /../../../..: " + Paths.get("/./././.").relativize(Paths.get("/../../../..")));
    System.out.println("./././. relativize ../../../..: " + Paths.get("./././.").relativize(Paths.get("../../../..")));
    System.out.println(
        "/./././. relativize ../../../..: java.lang.IllegalArgumentException: 'other' is different type of Path");
    System.out.println(
        "./././. relativize /../../../..: java.lang.IllegalArgumentException: 'other' is different type of Path");
    System.out.println("CAVEAT: Can't relativize absolute and relative paths together... just not possible");
    System.out
        .println("CAVEAT: It will consider the current . and going up .. as any given directory... not smart about it");

    System.out.println("Method prototype:          Path\tresolve(Path other)\n"
        + "Purpose:                   Resolve the given path against this path.\n");
    System.out.println("/tmp/foo resolve foo/bar/baz/quz/../../..: " + path2.resolve(path2b2));
    System.out.println("foo/bar/baz/quz/../../.. resolve foo/.: " + path2b2.resolve(path2b3));
    System.out.println("foo/. resolve foo/../foo: " + path2b3.resolve(path2b4));
    System.out.println("foo/../foo resolve /foo: " + path2b4.resolve(path2c));
    System.out.println("/foo resolve tmp/foo: " + path2c.resolve(path2d));
    System.out.println("tmp/foo resolve /tmp/foo/: " + path2d.resolve(path2e));
    System.out.println("/tmp/foo/ resolve /: " + path2e.resolve(pathRoot));
    System.out.println("/ resolve /.: " + pathRoot.resolve(pathRootCurrent));
    System.out.println("/. resolve (empty): " + pathRootCurrent.resolve(pathEmpty));
    System.out.println("(empty) resolve .: " + pathEmpty.resolve(pathEmptyCurrent));
    System.out.println(". resolve C:\\tmp: " + pathEmptyCurrent.resolve(pathWin));
    System.out.println("C:\\tmp resolve C:\\tmp: " + pathWin.resolve(pathWin));
    System.out.println("/./././. resolve /../../../..: " + Paths.get("/./././.").resolve(Paths.get("/../../../..")));
    System.out.println("./././. resolve ../../../..: " + Paths.get("./././.").resolve(Paths.get("../../../..")));
    System.out.println("/./././. resolve ../../../..: " + Paths.get("/./././.").resolve(Paths.get("../../../..")));
    System.out.println("./././. resolve /../../../..: " + Paths.get("./././.").resolve(Paths.get("/../../../..")));
    System.out.println("CAVEAT: if the path received is absolute, no matter what's this, it will get wiped!");
    System.out.println("CAVEAT: if the path received is relative, no matter what's this, it will get appended!");
    System.out
        .println("CAVEAT: It will consider the current . and going up .. as any given directory... not smart about it");

    System.out.println("Method prototype:          Path\tresolveSibling(Path other)\n"
        + "Purpose:                   Resolves the given path against this path's parent path.\n");
    System.out.println("/tmp/foo resolveSibling foo/bar/baz/quz/../../..: " + path2.resolveSibling(path2b2));
    System.out.println("foo/bar/baz/quz/../../.. resolveSibling foo/.: " + path2b2.resolveSibling(path2b3));
    System.out.println("foo/. resolveSibling foo/../foo: " + path2b3.resolveSibling(path2b4));
    System.out.println("foo/../foo resolveSibling /foo: " + path2b4.resolveSibling(path2c));
    System.out.println("/foo resolveSibling tmp/foo: " + path2c.resolveSibling(path2d));
    System.out.println("tmp/foo resolveSibling /tmp/foo/: " + path2d.resolveSibling(path2e));
    System.out.println("/tmp/foo/ resolveSibling /: " + path2e.resolveSibling(pathRoot));
    System.out.println("/ resolveSibling /.: " + pathRoot.resolveSibling(pathRootCurrent));
    System.out.println("/ resolveSibling .: " + pathRoot.resolveSibling(pathEmptyCurrent));
    System.out.println("/. resolveSibling (empty): " + pathRootCurrent.resolveSibling(pathEmpty));
    System.out.println("(empty) resolveSibling .: " + pathEmpty.resolveSibling(pathEmptyCurrent));
    System.out.println(". resolveSibling C:\\tmp: " + pathEmptyCurrent.resolveSibling(pathWin));
    System.out.println("C:\\tmp resolveSibling C:\\tmp: " + pathWin.resolveSibling(pathWin));
    System.out.println(
        "/./././. resolveSibling /../../../..: " + Paths.get("/./././.").resolveSibling(Paths.get("/../../../..")));
    System.out.println(
        "./././. resolveSibling ../../../..: " + Paths.get("./././.").resolveSibling(Paths.get("../../../..")));
    System.out.println(
        "/./././. resolveSibling ../../../..: " + Paths.get("/./././.").resolveSibling(Paths.get("../../../..")));
    System.out.println(
        "./././. resolveSibling /../../../..: " + Paths.get("./././.").resolveSibling(Paths.get("/../../../..")));
    System.out.println("CAVEAT: if the path received is absolute, no matter what's this, it will get wiped!");
    System.out.println(
        "CAVEAT: if the path received is relative, no matter what's this, it will get appended, but going up one before!");
    System.out.println("CAVEAT: if this is the root, careful!");
    System.out.println("CAVEAT: It will consider the current . and going up .. as any given directory... ");

    System.out.println("Method prototype:          boolean\tstartsWith(Path other)\n"
        + "Purpose:                   Tests if this path starts with the given path.\n");
    System.out.println("/tmp/foo startsWith foo/bar/baz/quz/../../..: " + path2.startsWith(path2b2));
    System.out.println("foo/bar/baz/quz/../../.. startsWith foo/.: " + path2b2.startsWith(path2b3));
    System.out.println("foo/../foo startsWith /foo: " + path2b4.startsWith(path2c));
    System.out.println("foo/../foo startsWith foo: " + path2b4.startsWith(path2b));
    System.out.println("foo/../foo startsWith foo/.: " + path2b4.startsWith(path2b3));
    System.out.println("/foo startsWith tmp/foo: " + path2c.startsWith(path2d));
    System.out.println("/foo startsWith foo: " + path2c.startsWith(path2b));
    System.out.println("foo startsWith foo/: " + path2b.startsWith(path2b1));
    System.out.println("tmp/foo startsWith /tmp/foo/: " + path2d.startsWith(path2e));
    System.out.println("/tmp/foo/ startsWith /: " + path2e.startsWith(pathRoot));
    System.out.println("/ startsWith /.: " + pathRoot.startsWith(pathRootCurrent));
    System.out.println("/ startsWith .: " + pathRoot.startsWith(pathEmptyCurrent));
    System.out.println("/. startsWith (empty): " + pathRootCurrent.startsWith(pathEmpty));
    System.out.println("(empty) startsWith .: " + pathEmpty.startsWith(pathEmptyCurrent));
    System.out.println(". startsWith C:\\tmp: " + pathEmptyCurrent.startsWith(pathWin));
    System.out.println("C:\\tmp startsWith C:\\tmp: " + pathWin.startsWith(pathWin));
    System.out
        .println("/./././. startsWith /../../../..: " + Paths.get("/./././.").startsWith(Paths.get("/../../../..")));
    System.out.println("./././. startsWith ../../../..: " + Paths.get("./././.").startsWith(Paths.get("../../../..")));
    System.out
        .println("/./././. startsWith ../../../..: " + Paths.get("/./././.").startsWith(Paths.get("../../../..")));
    System.out
        .println("./././. startsWith /../../../..: " + Paths.get("./././.").startsWith(Paths.get("/../../../..")));
    System.out.println("./ startsWith .: " + Paths.get("./").startsWith(Paths.get(".")));
    System.out.println("/ startsWith .: " + Paths.get("/").startsWith(Paths.get(".")));
    System.out.println("/ startsWith /.: " + Paths.get("/").startsWith(Paths.get("/.")));
    System.out.println("CAVEAT: ending slash is nothing");
    System.out.println("========================================================================================\n\n");

    System.out.println("========================================================================================");
    System.out.println("Method prototype:          Path\tsubpath(int beginIndex, int endIndex)\n"
        + "Purpose:                   Returns a relative Path that is a subsequence of the name elements of this path.\n"
        + "========================================================================================");
    System.out.println("/tmp/foo subpath 0, 1: " + path2.subpath(0, 1));
    System.out.println("/tmp/foo subpath 1, 2: " + path2.subpath(1, 2));
    System.out.println("/tmp/foo subpath 0, 2: " + path2.subpath(0, 2));
    System.out.println("foo/bar/baz/quz/../../.. subpath 0, 1: " + path2b2.subpath(0, 1));
    System.out.println("foo/bar/baz/quz/../../.. subpath 0, 7: " + path2b2.subpath(0, 7));
    System.out.println("foo/bar/baz/quz/../../.. subpath 1, 1: java.lang.IllegalArgumentException");// +
                                                                                                    // path2b2.subpath(1,
                                                                                                    // 1));
    System.out.println("foo/../foo subpath 0, 1: " + path2b4.subpath(0, 1));
    System.out.println("foo/../foo subpath 1, 1: java.lang.IllegalArgumentException");// + path2b4.subpath(1, 1));
    System.out.println("foo/../foo subpath 1, 2: " + path2b4.subpath(1, 2));
    System.out.println("/foo subpath 0, 1: " + path2c.subpath(0, 1));
    System.out.println("/foo subpath 0, 1: " + path2c.subpath(0, 1));
    System.out.println("foo subpath 0, 1: " + path2b.subpath(0, 1));
    System.out.println("tmp/foo subpath 0, 1: " + path2d.subpath(0, 1));
    System.out.println("/tmp/foo/ subpath 0, 1: " + path2e.subpath(0, 1));
    System.out.println("/ subpath 0, 1: java.lang.IllegalArgumentException");// + pathRoot.subpath(0, 1));
    System.out.println("/ subpath 0, 1: java.lang.IllegalArgumentException");// + pathRoot.subpath(0, 1));
    System.out.println("/. subpath 0, 1: " + pathRootCurrent.subpath(0, 1));
    System.out.println("(empty) subpath 0, 1: " + pathEmpty.subpath(0, 1));
    System.out.println(". subpath 0, 1: " + pathEmptyCurrent.subpath(0, 1));
    System.out.println("C:\\tmp subpath 0, 1: " + pathWin.subpath(0, 1));
    System.out.println("/./././. subpath 0, 1: " + Paths.get("/./././.").subpath(0, 1));
    System.out.println("/./././. subpath 0, 4: " + Paths.get("/./././.").subpath(0, 4));
    System.out.println("./././. subpath 0, 1: " + Paths.get("./././.").subpath(0, 1));
    System.out.println("/./././. subpath 0, 1: " + Paths.get("/./././.").subpath(0, 1));
    System.out.println("./././. subpath 0, 1: " + Paths.get("./././.").subpath(0, 1));
    System.out.println("./ subpath 0, 1: " + Paths.get("./").subpath(0, 1));
    System.out.println("/ subpath 0, 1: java.lang.IllegalArgumentException");// + Paths.get("/").subpath(0, 1));
    System.out.println("/ subpath 0, 1: java.lang.IllegalArgumentException");// + Paths.get("/").subpath(0, 1));
    System.out.println("CAVEAT: root is not taken into account for anything");
    System.out.println("CAVEAT: can't subpath with two equal indexes");
    System.out.println("========================================================================================\n\n");

  }
}

Related Topic