I wish to use a string handed to me as a parameter as an element of a pathname. I do not wish to be troubled with 'Little Bobby Tables'. That is, I don't want '..' to be acceptable. I want this to work with an arbitrary NIO2 FileSystem
, so just looking for the string ..
isn't good enough. I can make some checks, such as !path.isAbsolute()
, and path.getNameCount() == 1
to filter out other problem cases, but can I do anything other than pass it to resolve
and look for the wrong name count to tell if it has the semantics of ..
?
toRealPath()
Is mostly the answer. The method on a path after resolved gives you the real path after resolving parent (e.g. "..") and links.
You can then use root.startsWith( path ) to quickly check that it is in your subtree.
But: Works only for existing files.