您将如何解决相对路径?我要找的是类似于php的realpath的功能。该函数只需要删除所有 .././以便输入字符串可以安全地与其他字符串一起使用。
最佳答案
一般的方法是使用File类getCanonicalPath()方法。
它专门用于删除(解析)您要查找的 ../
和 ./
。
文档摘录:
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).
关于Java 解析相对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5121967/