java获取文件夹 路径,Java获取文件的路径
本文記錄的是如何獲取資源文件的路徑.
先看看我設置的文件目錄結構,如下圖所示:
___
Java Build Path的設置如下圖所示, 主要看build project之后的文件輸出目錄:
___
在Java中有兩種方式可以獲取到文件的路徑,通過下面的測試代碼看看它們的不同:
1
2
3
4
5String resPath = this.getClass().getResource("").getPath();
System.out.println("resPath: " + resPath);
String resPath1 = this.getClass().getClassLoader().getResource("").getPath();
System.out.println("resPath1: " + resPath1);
控制臺中輸出結果如下:
1
2resPath: /Users/carya/dev/RwProperties/target/classes/edu/cugb/tester/
resPath1: /Users/carya/dev/RwProperties/target/classes/
從結果中可以看到,使用this.getClass().getResource()獲得的是代碼所在類編譯成class文件之后輸出文件所在目錄位置,而this.getClass().getClassLoader().getResource()獲得的是class loader所在路徑,該函數查看Java doc解釋如下:
Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code. The name of a resource is a ‘/’-separated path name that identifies the resource.
This method will first search the parent class loader for the resource; if the parent is null the path of the class loader built-in to the virtual machine is searched. That failing, this method will invoke findResource(String) to find the resource.
使用this.getClass().getResource(String name)函數時,name參數可以使用文件的絕對路徑,也可以使用相對路徑,如下所示:
1
2
3
4
5String resPath2 = this.getClass().getResource("../../../jdbc.properties").getPath();
System.out.println("resPath2: " + resPath2);
String resPath3 = this.getClass().getResource("/hbm/setting.txt").getPath();
System.out.println("resPath3: " + resPath3);
上述事例控制臺的輸出是:
1
2resPath2: /Users/carya/dev/RwProperties/target/classes/jdbc.properties
resPath3: /Users/carya/dev/RwProperties/target/classes/hbm/setting.txt
而使用this.getClass().getClassLoader().getResource(String name)函數時,name參數則只能使用相對于class loader目錄的路徑,即相對路徑,看下面的事例:
1
2
3
4
5String resPath4 = this.getClass().getClassLoader().getResource("./hbm/setting.txt").getPath();
System.out.println("resPath4: " + resPath4);
String resPath5 = this.getClass().getClassLoader().getResource("/hbm/setting.txt").getPath();
System.out.println("resPath5: " + resPath5);
控制臺輸出如下, resPath4使用的是相對路徑,能夠得到正確結果,而resPath5使用的是絕對路徑,拋出Exception:
1
2
3
4resPath4: /Users/carya/dev/RwProperties/target/classes/hbm/setting.txt
Exception in thread "main" java.lang.NullPointerException
at edu.cugb.tester.PropertiesPath.getFilePath(PropertiesPath.java:39)
at edu.cugb.tester.PropertiesPath.main(PropertiesPath.java:54)
另外,Thread.currentThread().getContextClassLoader().getResource()也可以獲取資源文件路徑,在獲取資源路徑方面其使用與this.getClass().getClassLoader().getResource()相似:
1
2
3
4
5String resPath6 = Thread.currentThread().getContextClassLoader().getResource("").getPath();
System.out.println("resPath6: " + resPath6);
String resPath7 = Thread.currentThread().getContextClassLoader().getResource("./jdbc.properties").getPath();
System.out.println("resPath7: " + resPath7);
控制臺的輸出如下:
1
2resPath6: /Users/carya/dev/RwProperties/target/classes/
resPath7: /Users/carya/dev/RwProperties/target/classes/jdbc.properties
總結
以上是生活随笔為你收集整理的java获取文件夹 路径,Java获取文件的路径的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 恨一个人的网名121个
- 下一篇: 日语网名女生91个