Path of "." and ""

Francis Galiegue fgaliegue at gmail.com
Mon Dec 8 09:55:20 UTC 2014


On Mon, Dec 8, 2014 at 10:37 AM, Wang Weijun <weijun.wang at oracle.com> wrote:
> Path p1 = Paths.get(".");
> Path p2 = Paths.get("./s");
>
> Then p2.startsWith(p1) is true.
>
> If I normalize them, p1 becomes "", p2 becomes "s", and p2.startsWith(p1) is false.
>
> I know it's because after normalizing, p1 has an element which is "", and p2 has an element which is "s", therefore one array does not starts with the other. Maybe p1 should be normalized to no element at all?
>

Indeed, it seems logical that p1 has no elements at all, nothing, not
even a root here.

But then you are referring to UnixPath, right? My own Path
implementation does not use one array for the whole name, but one
element for the root and one element per name component (in fact, it
is so generic that it is final, no less -- and it can even have empty
name elements should there be some bizarre fs out there which accepts
that). The end result is the same anyway: it gives the same results as
you, which is logical.

> And what is the best way to check if one path is inside another? I do not want to call toAbsolutePath().
>

With Unix that would even be .toRealPath() (yes, it throws an IOE).
But that proves that Path is abstract and you don't really know that
it is something real unless you "dereference" it... And once you have
done that, collect all elements into Lists and call
Collections.indexOfSublist()?

-- 
Francis Galiegue, fgaliegue at gmail.com, https://github.com/fge
JSON Schema in Java: http://json-schema-validator.herokuapp.com
Parsers in pure Java: https://github.com/parboiled1/grappa (redde
Caesaris: https://github.com/sirthias)


More information about the nio-dev mailing list