獲取專案中的檔案路徑

2021-10-07 06:09:22 字數 657 閱讀 1265

必須在類路徑src下(目錄中不能有中文和空格)

統一的方式,提高**的可移植性(linux和windows都可)

// getresource("")已經定位到 src 目錄,只需要填入該檔案的相對於src的目錄

string path = thread.

currentthread()

.getcontextclassloader()

.getresource

("檔案相對路徑").

getpath()

;

resourcebundle獲取properties檔案

只能繫結***.properties檔案,並且檔案只能在類路徑下,在寫路徑的時候不能寫拓展名

// 在src目錄下的classinfo.properties檔案,直接以流的方式返回

inputstream in = resourcebundle bundle = resourcebundle.

getbundle

("classinfo"

)

web專案中各種路徑的獲取

絕對路徑 絕對路徑就是你的主頁上的檔案或目錄在硬碟上真正的路徑,url和物理路徑 例如 c xyz test.txt 代表了test.txt檔案的絕對路徑。也代表了乙個url絕對路徑。相對路徑 相對與某個基準目錄的路徑。包含web的相對路徑 html中的相對目錄 例如 在servlet中,代表web...

獲取專案中的檔案方法

tomcat環境獲取檔案方法 string htmlfile request.getservletcontext getrealpath html zhantu.html 下面是我在jersey框架中開發的 springboot環境獲取resouces下的檔案方法 resourceutils.get...

Java 專案中獲取路徑的方法小計

getclass getresource path path為 開頭時是從classpath下尋找,否則從當前類所在路徑尋找 getclassloader getresource path 從classpath下尋找 getresource path 返回路徑上匹配的第乙個資源 getresourc...