1. 程式人生 > >java載入檔案目錄

java載入檔案目錄

// 第一種:獲取類載入的根路徑  D:\Git\daotie\daotie\target\classes  File f = new File(this.getClass().getResource("/").getPath());  System.out.println(f);  // 獲取當前類的所在工程路徑; 如果不加“/”  獲取當前類的載入目錄  D:\git\daotie\daotie\target\classes\my  File f2 = new File(this.getClass().getResource("").getPath());  System.out.println(f2);  // 第二種:獲取專案路徑  D:\git\daotie\daotie  File directory = new File("");// 引數為空  String courseFile = directory.getCanonicalPath();  System.out.println(courseFile); .getParentFile()父路徑  // 第三種:  file:/D:/git/daotie/daotie/target/classes/  URL xmlpath = this.getClass().getClassLoader().getResource("");  System.out.println(xmlpath);

 // 第四種: D:\git\daotie\daotie  System.out.println(System.getProperty("user.dir"));  /*  * 結果: C:\Documents and Settings\Administrator\workspace\projectName  * 獲取當前工程路徑  */  // 第五種:  獲取所有的類路徑 包括jar包的路徑  System.out.println(System.getProperty("java.class.path"));