天天看點

java中得到classpath和目前類的絕對路徑的一些方法(路徑中的%20"進行替換空格)

原網址:http://blog.csdn.net/shendl/article/details/1427475

(注意:利用下面方式得到路徑,如果路徑中有空格字元, 那麼會有"%20"進行替換, 是以得到之後再将"%20"替換成空格)

下面是一些得到classpath和目前類的絕對路徑的一些方法。你可能需要使用其中的一些方法來得到你需要的資源的絕對路徑。

1,filetest.class.getresource("");

     filetest.class.getresource("../");//得到上一級

得到的是目前類filetest.class檔案的uri目錄。不包括自己!

如:file:/d:/java/eclipse32/workspace/jbpmtest3/bin/com/test/

2,filetest.class.getresource("/")

得到的是目前的classpath的絕對uri路徑。

如:file:/d:/java/eclipse32/workspace/jbpmtest3/bin/

3,thread.currentthread().getcontextclassloader().getresource("")

得到的也是目前classpath的絕對uri路徑。

如:file:/d:/java/eclipse32/workspace/jbpmtest3/bin/

4,filetest.class.getclassloader().getresource("")

得到的也是目前classpath的絕對uri路徑。

如:file:/d:/java/eclipse32/workspace/jbpmtest3/bin/

5,classloader.getsystemresource("")

得到的也是目前classpath的絕對uri路徑。

如:file:/d:/java/eclipse32/workspace/jbpmtest3/bin/

我推薦使用thread.currentthread().getcontextclassloader().getresource("")來得到目前的classpath的絕對路徑的uri表示法。