溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

java獲取文件路徑的方式有幾種

發(fā)布時間:2020-06-10 18:46:38 來源:億速云 閱讀:238 作者:鴿子 欄目:編程語言

                                                           

第一種:

File f = new File(this.getClass().getResource("/").getPath());
System.out.println(f);

結果:

C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin

獲取當前類的所在工程路徑。

如果不加“/”

File f = new File(this.getClass().getResource("").getPath());
System.out.println(f);

結果:

C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test

獲取當前類的絕對路徑。

第二種:

File directory = new File("");//參數(shù)為空
String courseFile = directory.getCanonicalPath();
System.out.println(courseFile);

結果:

C:\Documents and Settings\Administrator\workspace\projectName

獲取當前類的所在工程路徑。

第三種:

URL xmlpath = this.getClass().getClassLoader().getResource("selected.txt");
System.out.println(xmlpath);

結果:

file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt

獲取當前工程src目錄下selected.txt文件的路徑。

第四種:

System.out.println(System.getProperty("user.dir"));

結果:

C:\Documents and Settings\Administrator\workspace\projectName

獲取當前工程路徑。

第五種:

System.out.println( System.getProperty("java.class.path"));

結果:

C:\Documents and Settings\Administrator\workspace\projectName\bin

獲取當前工程路徑。

以上就是java如何獲取文件路徑的詳細內容,更多請關注億速云其它相關文章!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經查實,將立刻刪除涉嫌侵權內容。

AI