This code works:
public final class Foo { private static final List<String> INPUTS = Arrays.asList( "/foo", "//foo", "foo/", "foo/bar", "foo/bar/../baz", "foo//bar" ); public static void main(final String... args) { Path path; for (final String input: INPUTS) { path = Paths.get("/", input).normalize(); System.out.printf("%s -> %s\n", input, path); } } }
Conclusion:
/foo -> /foo //foo -> /foo foo/ -> /foo foo/bar -> /foo/bar foo/bar/../baz -> /foo/baz foo
Please note that this is NOT portable. It will not work on Windows machines ...
If you want to use a portable solution, you can use the memory file system , open the Unix file system and use it:
try ( final FileSystem fs = MemoryFileSystem.newLinux().build(); ) {
source share