Java allows relative path

How do you feel about resolving a relative path? What I'm looking for is a function similar to php realpath. The function just needs to delete everything .. /./ so that the input string can be safely used with other strings.

+6
java
source share
3 answers

The general way is to use the File getCanonicalPath () method.

+9
source share

Since you mentioned PHP, I will take the web context. Using the Servlet API, you can get the real path corresponding to the relative path using servletContext.getRealPath(relativePath)

Outside of the web context, you can use file.getAbsolutePath() , where file is java.io.File built with a relative path.

+4
source share

I know that this is not yet in production, but in Java 7 one could call Path.resolve (Path) .

Just heads up.

+2
source share

All Articles