servlet讀取資源檔案的三種方式

2021-07-03 15:37:33 字數 702 閱讀 2663

1.通過相對路徑得到絕對路徑:

string 

path = this.getservletcontext.getrealpath(資源路徑)//路徑針對於webroot下

inputstream in = new fileinputstream(path);

properties prop = new properties();

prop.prop.load(in);

prop.getproperty(key);

2.直接通過相對路徑:

inputstream in = 

this.getservletcontext.getrealpath(資源路徑).getresourceasstream();

properties prop = new properties();

prop.prop.load(in);

prop.getproperty(key);

3.通過類載入器:

inputstream in = this.getclass().getclassloader().getreasourceasstream(資源路徑);//資源路徑如果放在src下面直接寫資源檔案的名字即可

properties prop = new properties();

prop.prop.load(in);

prop.getproperty(key);

讀取資源檔案

資源檔案在存放彈出資訊很有用,以下是讀資源檔案的簡單 dim rm as resourcemanager new resourcemanager readresx.message system.reflection.assembly.getexecutingassembly dim ci as cu...

c 讀取資源檔案

全域性資源檔案 getglobalresourceobject string param1,string param2 讀取全域性資源檔案的方法 引數說明 param1 資源檔案類名 即資源檔名,不帶resx param2 鍵值名 區域性資源檔案 getlocalresourceobject str...

讀取properties資源檔案

方法一 在servlet中利用servletcontext物件讀取配置檔案 this.getservletcontext getresource web inf classes db.properties inputstream in url.openstream properties prop n...