Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Resolve Relative Path

Tags:

java

How would you go about resolving a relative path? What I am looking for is a function similar to php's realpath. The function just needs to remove all ../ ./ so that the input string can be safely used with other strings.

like image 612
Pass Avatar asked Feb 25 '11 19:02

Pass


1 Answers

The general way is to use the File class getCanonicalPath() method.
It's specifically documented to remove (resolve) the ../ and ./ that you're looking for.

Extracts from the docs:

This method first converts this pathname to absolute form if necessary [...] and then maps it to its unique form in a system-dependent way. This typically involves removing redundant names such as "." and ".." from the pathname, resolving symbolic links (on UNIX platforms), and converting drive letters to a standard case (on Microsoft Windows platforms).

like image 65
Stephen P Avatar answered Nov 02 '22 05:11

Stephen P