Determine if `.` and `..` are "special names" in a Java file system

Viewed 38

I've been accustomed to thinking that . and .. are always filenames with special meanings. But some Java 11 APIs such as Path.normalize() indicate that this is true only on some file system implementations:

In many file systems, the "." and ".." are special names used to indicate the current directory and parent directory.

If . and .. are "special names" only on some file systems, how can I query the file system to know if those names are special for that FileSystem implementation?

1 Answers

There is no specification that requires a FileSystem implementation to give you this information.

So even if some FileSystem would give you this information you cannot be sure if you are not dealing with a file system that does not give you this information.

If you really care about this information and need it for your code to work correctly your only option is something along the line

Map<String, Object> env = new HashMap<>();
FileSystem f = FileSystems.newFileSystem(new URI("rw:///"), env);

boolean equalsDoesntNormalize = !f.getPath("a/./b").equals(f.getPath("a/b"));
boolean equalAfterNormalize = f.getPath("a/./b").normalize().equals(f.getPath("a/b"));
boolean dotIsSpecial = equalAfterNormalize && !equalsDoesntNormalize;

But, to be honest: I don't think that you need this information...

Related