使用java代碼讀取properties文件,在new File的時候突然發現直接使用相對路徑不管用了,之前做開發因為業務以及工作內容沒有涉及到這塊,也沒有太關注這方面的東西,這次做一個相對比較全的分析。
1、可以通過類的class文件路徑獲知當前項目或者編譯文件的路徑
在Class類里面有這樣一個方法getResource(String name)
查找帶有給定名稱的資源獲取一個URL

public static void main(String[] args) throws IOException { //獲取當前類class所在的resource路徑 System.out.println(TestFilePath.class.getResource("/").getPath()); //獲取當前類class所在的路徑: System.out.println(TestFilePath.class.getResource("").getPath()); }
代碼輸出結果如下,我們可以很明顯的知道加載資源文件是需要resource根目錄的,一般我們使用帶斜杠的
/E:/develop/workspace/selfproject/nutrition/target/test-classes/ /E:/develop/workspace/selfproject/nutrition/target/test-classes/cn/test/
獲取這個路徑后還要直接拼接資源文件路徑,可以直接通過TestFilePath.class.getResource("/request-mapping.properties").getPath()方式獲取
2、通過new File("")來確定工程目錄(非編譯運行文件的目錄)

public static void main(String[] args) throws IOException { File f = new File(""); System.out.println("空字符file的Path : " + f.getPath()); System.out.println("空字符file的標准路徑CanonicalPath : " + f.getCanonicalPath()); System.out.println("空字符file的絕對路徑AbsolutePath : " + f.getAbsolutePath()); }
代碼輸出結果如下,可以根據資源文件在工程目錄對應的路徑進行資源路徑的拼接,從而完成資源的定位(同時也可以通過直接new File(path)的方式獲取文件,其中path是相對於工程目錄的路徑)
空字符file的Path :
空字符file的正則路徑CanonicalPath : E:\develop\workspace\selfproject\nutrition
空字符file的絕對路徑AbsolutePath : E:\develop\workspace\selfproject\nutrition
3、通過類加載的路徑來獲取對應的路徑信息

public static void main(String[] args) throws IOException { URL xmlpath = new TestFilePath().getClass().getClassLoader().getResource("/"); URL xmlpath1 = new TestFilePath().getClass().getClassLoader().getResource(""); System.out.println("獲取當前類被加載的工程路徑:" + xmlpath); System.out.println("獲取當前類被加載的路徑:" + xmlpath1); }
代碼輸出結果如下,結果說明類加載獲取resource時只能用getResource(""),這是因為加載類的路徑就默認是在根目錄下的,加斜杠反而無法獲取路徑(將斜杠當成資源名稱了)
獲取當前類被加載的工程路徑:null 獲取當前類被加載的路徑:file:/E:/develop/workspace/selfproject/nutrition/target/test-classes/
4、直接通過java的系統System類獲取項目路徑

public static void main(String[] args) throws IOException { System.out.println(System.getProperty("user.dir")); }
代碼輸出結果如下,獲取的是項目的根目錄
E:\develop\workspace\selfproject\nutrition
補充,這里可以通過system獲取的對應信息可以都了解下

public static void main(String[] args) throws IOException { Properties p = System.getProperties(); Set<Entry<Object, Object>> entrys = p.entrySet(); for (Entry<Object, Object> entry : entrys) { System.out.println(entry.getKey().toString() + " : " + entry.getValue().toString()); } }
打印結果:

java.runtime.name : Java(TM) SE Runtime Environment sun.boot.library.path : C:\Program Files\Java\jdk1.7.0_79\jre\bin java.vm.version : 24.79-b02 java.vm.vendor : Oracle Corporation java.vendor.url : http://java.oracle.com/ path.separator : ; java.vm.name : Java HotSpot(TM) 64-Bit Server VM file.encoding.pkg : sun.io user.country : CN user.script : sun.java.launcher : SUN_STANDARD sun.os.patch.level : java.vm.specification.name : Java Virtual Machine Specification user.dir : E:\develop\workspace\selfproject\nutrition java.runtime.version : 1.7.0_79-b15 java.awt.graphicsenv : sun.awt.Win32GraphicsEnvironment java.endorsed.dirs : C:\Program Files\Java\jdk1.7.0_79\jre\lib\endorsed os.arch : amd64 java.io.tmpdir : C:\Users\Admin\AppData\Local\Temp\ line.separator : java.vm.specification.vendor : Oracle Corporation user.variant : os.name : Windows 8.1 sun.jnu.encoding : GBK java.library.path : C:\Program Files\Java\jdk1.7.0_79\bin;C:\WINDOWS\Sun\Java\bin;C:\WINDOWS\system32;C:\WINDOWS;C:\ProgramData\Oracle\Java\javapath;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\IDM Computer Solutions\UltraEdit;C:\Program Files\IDM Computer Solutions\UltraCompare;D:\develop\nexus-2.14.5-02-bundle\nexus-2.14.5-02\bin;C:\Program Files\IDM Computer Solutions\UltraEdit;D:\develop\apache-maven-3.5.2\bin;C:\Program Files\Java\jdk1.7.0_79\bin;C:\Program Files\Java\jdk1.7.0_79\jre\bin;C:\Program Files\MySQL\MySQL Utilities 1.6\;C:\Users\Admin\AppData\Local\Microsoft\WindowsApps;. java.specification.name : Java Platform API Specification java.class.version : 51.0 sun.management.compiler : HotSpot 64-Bit Tiered Compilers os.version : 6.3 user.home : C:\Users\Admin user.timezone : java.awt.printerjob : sun.awt.windows.WPrinterJob file.encoding : UTF-8 java.specification.version : 1.7 java.class.path : E:\develop\workspace\selfproject\nutrition\target\test-classes;E:\develop\workspace\selfproject\nutrition\target\classes;D:\develop\apache-maven-3.5.2\self-repo\log4j\log4j\1.2.9\log4j-1.2.9-sources.jar;D:\develop\apache-maven-3.5.2\self-repo\javax\servlet\servlet-api\2.5\servlet-api-2.5.jar user.name : Admin java.vm.specification.version : 1.7 sun.java.command : cn.test.TestFilePath java.home : C:\Program Files\Java\jdk1.7.0_79\jre sun.arch.data.model : 64 user.language : zh java.specification.vendor : Oracle Corporation awt.toolkit : sun.awt.windows.WToolkit java.vm.info : mixed mode java.version : 1.7.0_79 java.ext.dirs : C:\Program Files\Java\jdk1.7.0_79\jre\lib\ext;C:\WINDOWS\Sun\Java\lib\ext sun.boot.class.path : C:\Program Files\Java\jdk1.7.0_79\jre\lib\resources.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\rt.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\sunrsasign.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jce.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.7.0_79\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.7.0_79\jre\classes java.vendor : Oracle Corporation file.separator : \ java.vendor.url.bug : http://bugreport.sun.com/bugreport/ sun.io.unicode.encoding : UnicodeLittle sun.cpu.endian : little sun.desktop : windows sun.cpu.isalist : amd64
https://blog.csdn.net/theonegis/article/details/23695057