JAVA 類檔案獲取路徑

2021-04-28 21:14:02 字數 456 閱讀 6274

windows , linux 下通用。

當前工程的目錄是 d:/www_jsp/projectabc,可以使用以下3個方法

1、在工程中的任何乙個 類中,可以這樣獲取路徑:

通過 this.getclass().getclassloader().getresource(".").getpath();

返回的是編譯之後的classes檔案的根目錄——

/d:/www_jsp/projectabc/webcontent/web-inf/classes/

2、通過 string str = new file(".").getabsolutepath();

返回的是 d:/www_jsp/projectabc/.

3、通過 string str = new file(".").getcanonicalpath();

返回的是 d:/www_jsp/projectabc

java中獲取路徑 獲取檔案方法

1 system.getproperty user.dir 獲取使用者的當前工作路徑 2 class.getprotectiondomain getcodesource getlocation getpath 獲取jar包路徑位址 不過有兩個需要注意的 1.返回如果是個檔案說明獲取的是jar包完整路...

Java根據路徑獲取檔案內容

首先,給出乙個string表示資源檔案的標識,如何判斷是網路中的檔案還是本地的檔案?http開頭的可以看成是網路檔案 其餘的可看成本地檔案對於mac和linux系統而言 以 和 開頭的表示絕對路徑 其他的看做是相對路徑 對於windows系統而言,絕對路徑形如c test.text 路徑中包含 看成...

java讀取文獲取檔案路徑

string directory d 呵呵 3059 此目錄下放9張 listf new arraylist file dir new file directory if dir.isfile return 判斷是否是檔案,如果是檔案則返回。file files dir.listfiles 列出當前...