java相对路径怎么转为绝对路径

Python018

java相对路径怎么转为绝对路径,第1张

java.io.File提供了两个方法:

File

getAbsoluteFile()返回一个File对象实例

Returns the absolute form of this abstract pathname.

String

getAbsolutePath() 返回一个字符串

Returns the absolute pathname string of this abstract pathname.

java.nio.file.Path提供的

Path

toAbsolutePath()

Returns a Path object representing the absolute path of this

path.

服务器中的Java类获得当前路径

Weblogic WebApplication的系统文件根目录是你的weblogic安装所在根目录。

例如:如果你的weblogic安装在c:\bea\weblogic700…… 那么,你的文件根路径就是c:\. 所以,有两种方式能够让你访问你的服务器端的文件:

a.使用绝对路径: 比如将你的参数文件放在c:\yourconfig\yourconf.properties, 直接使用 new FileInputStream("yourconfig/yourconf.properties");

b.使用相对路径: 相对路径的根目录就是你的webapplication的根路径,即WEB-INF的上一级目录,将你的参数文件放

在yourwebapp\yourconfig\yourconf.properties, 这样使用: new

FileInputStream("./yourconfig/yourconf.properties"); 这两种方式均可,自己选择。

(2)。Tomcat 在类中输出System.getProperty("user.dir");显示的是%Tomcat_Home%/bin

(3)。Resin 不是你的JSP放的相对路径,是JSP引擎执行这个JSP编译成SERVLET 的路径为根。比如用新建文件法测试File f = new File("a.htm"); 这个a.htm在resin的安装目录下

(4)。如何读相对路径哪? 在Java文件中getResource或getResourceAsStream均可

例:getClass()。getResourceAsStream(filePath);//filePath可以是"/filename",这

里的/代表web 发布根路径下WEB-INF/classes 默认使用该方法的路径是:WEB-INF/classes.已经在Tomcat中测试。