I have a file path like this:
/home/Dara/Desktop/foo/bar/baz/qux/file.txt
In Java, I would like to be able to get the top two folders. Ie. baz/qux
regardless of file path length or operating system (File path separators such as /
:
and \
). I have tried to use the subpath()
method in Paths
but I can't seem to find a generic way to get the length of the file path.
Path.subpath()
? – InterimPath.subpath(0, 2)
gives mehome/Dara
. I would like to be able to say something likePath.subpath(myPath.length() - 3, myPath.length() - 1
to getbaz/qux
. – DaytodayPath.getNameCount()
for the length? – Interim