java各種獲取資源檔案的方式

2021-08-27 21:44:14 字數 2092 閱讀 3544

/*** 從絕對路徑讀取檔案,最基本的檔案讀取方式

* * @author lihzh

* @data 2012-4-11 下午9:33:44

*/@test

public

void

testgetfilefromabsolutepath()  

/*** 對資料夾型別檔案的斷言

* * @param dirfile

* @author lihzh

* @data 2012-4-11 下午9:49:14

*/private

void

assertdirfile(file dirfile)  

/*** 從相對路徑讀取檔案,相對於編譯路徑,在eclipse中即為工程所在根目錄。 本質還是絕          * 對路徑讀取。

* * @author lihzh

* @data 2012-4-11 下午9:51:10

*/@test

public

void

testgetfilefromrelativepath()  

/*** 構造uri/url格式的檔案路徑,讀取本地檔案

* * @author lihzh

* @throws urisyntaxexception

* @throws malformedurlexception

* @data 2012-4-11 下午10:25:00

*/@test

public

void

testgetfilefromuripath() 

throws

urisyntaxexception, 

malformedurlexception  

特別說明:用uri/url的方式構造路徑是個人比較推薦的,可以解決一些路徑讀取的問題。例如:spring會對uri/url格式的路徑進行專有處理可以準確定位的位置,而直接使用絕對路徑,在用classpath和filesystem兩種不同的初始化方式下,可能會出現錯誤。

/*** 從依賴的jar包中讀取檔案, jar包內的檔案是無法用file讀取的,只能用stream的方式讀取。

* * @author lihzh

* @throws urisyntaxexception

* @throws ioexception

* @data 2012-4-11 下午11:07:58

*/@test

public

void

testgetfilefromjarinclasspath() 

throws

urisyntaxexception, 

ioexception  

/*** 從classpath中的jar包讀取某資料夾下的所有檔案

* * @author lihzh

* @throws ioexception 

* @data 2012-4-13 下午10:22:24

*/@test

public

void

testgetfilesfromjarinclasspathwithdirpath() 

throws

ioexception  

} assert.asserttrue(props.containskey("test.key"

)); 

assert.assertequals("thisisvalue"

, props.getproperty(

"test.key"

)); 

assert.asserttrue(props.containskey("test.key.two"

)); 

assert.assertequals("thisisanothervalue"

, props.getproperty(

"test.key.two"

)); 

} 對於不在classpath下的jar包的讀取,當作乙個本地檔案用jarfile讀取即可。路徑可使用絕對路徑。或者用上面的url.getconnection也可以處理。這裡不再實現。

ps:附件是測試用的jar包。

C 中獲取資源檔案

1.可以在系統自動建立的資源檔案中新增物件 2.獲取的時候用properties.resources.3 可以自己建立資源檔案,例如stocksql.resx 用來存放sql,新增資源testsql 4.通過stocksql.testsql來獲取,系統其實是通過在properties.resourc...

iOS 獲取資源檔案路徑

1 documents 目錄 您應該將所有 de應用程式資料檔案寫入到這個目錄下。這個目錄用於儲存使用者資料或其它應該定期備份的 資訊。這是應用程式 的程式包目錄,包含應用程式 的本身。由於應用程式必須經過簽名,所以您在執行時不能對這個目錄中 的內容進行修改,否則可能會使應用程式無法啟動。3 lib...

按文化設定獲取資源檔案

using system.reflection using system.resources using system.threading using system.globalization 3 獲取資源檔案管理器 resourcemanager rm new resourcemanager wi...