Reputation: 64925
Given a path like /a/./b/c/../d
, I would like to remove all the "current directory" indicators (i.e., period) and "parent directory" indicators (i.e., ..), giving a/b/d
.
I could use File.getCanonicalPath()
, but that also resolves symlinks, which I don't want.
Any simple way? That is, simpler than writing a tokenizer and handling it all myself.
Bonus points if you can tell me what the right name for '.' and '..' are in this context.
Upvotes: 15
Views: 7214
Reputation: 787
Path.normalize does what you're looking for, and does not require any third party. It is available since jdk8.
Paths.get("/a/./b/c/../d").normalize()
=> /a/b/d
Be careful, however, that it will resolve the path according to the current OS. Executing the same command on Windows yields a different result.
Paths.get("/a/./b/c/../d").normalize()
=> \a\b\d
Upvotes: 1
Reputation: 27607
You can use FilenameUtils.normalize()
in the Apache Commons IO library - see javadoc here.
"Dot" (.
) is the current directory and "dot dot" (..
) is the parent directory - read up on unix directories.
Upvotes: 5
Reputation: 110054
Guava also has this as Files.simplifyPath(String)
. Your best bet, though, (if you can use JDK7) is to represent your path as a Path
and use Path.normalize()
to get the normalized version.
Upvotes: 13