1. 程式人生 > >Java路徑類問題總結

Java路徑類問題總結

back ava admin .get ron ade pro tro pri

一.獲取路徑:

單獨的Java類中獲得絕對路徑根據java.io.File的Doc文擋,可知: 默認情況下new File("/")代表的目錄為:System.getProperty("user.dir")。
一下程序獲得執行類的當前路徑:

System.out.println("1:" + Test2.class.getResource(""));
System.out.println("2:" + Test2.class.getClassLoader().getResource(""));
System.out.println("3:" + Test2.class.getClassLoader().getResource("").getPath());
System.out.println(
"4:" +ClassLoader.getSystemResource(""));


輸出結果為:

1:file:/C:/Users/Administrator/git/sourceControl/demo1/target/test-classes/demo1/
2:file:/C:/Users/Administrator/git/sourceControl/demo1/target/test-classes/
3:/C:/Users/Administrator/git/sourceControl/demo1/target/test-classes/
4:file:/C:/Users/Administrator/git/sourceControl/demo1/target/test-classes/

Java路徑類問題總結