android讀取資源檔案的方法

2021-06-17 15:24:22 字數 1420 閱讀 6460

方法一:把目標檔案放入resources檔案中,以通過讀取r的資源檔案來獲取,具體方式如下:

1、在res下新建raw檔案,將帶讀取檔案新增到raw檔案目錄下。

2、新增如下**:

// 如果要使用檔名獲取檔案資料:首先獲取資源id然後再通過id獲取輸入流

/** string filename = filename;

string packetname = context.getpackagename();

//將filename 轉換成id 

int resid = context.getresources().getidentifier(filename, "raw", packetname);

objectinputstream ois = null;

inputstream im = context.getresources().openrawresource(resid); 

//其中getidentifier三引數分別是:檔名,資源所在資料夾名(如:drawable, raw,),包路徑 

*/inputstream im = getresources().openrawresource(r.raw.h_data11);

bufferedreader read = new bufferedreader(new inputstreamreader(im));

string line = "";

stringbuilder sb = new stringbuilder();

try

} catch (ioexception e) finally catch (ioexception e)

}if(im != null) catch (ioexception e) }}

log.v("", "result = " + sb.tostring());

複製**

方法二:使用assets 唯讀檔案進行讀取。

1、將檔案copy到assets下,可以新建資料夾如:「www」然後將檔案放入www資料夾中,讀取的path為:"www/filename"

string result = "";

objectinputstream ois = null;

assetmanager am = context.getresources().getassets();

try catch (streamcorruptedexception e) catch (filenotfoundexception e) catch (ioexception e) catch (classnotfoundexception e) finally

} catch (ioexception e) }

複製**

以物件的方式讀取檔案中的資料,如果沒有新建資料夾,把前面的「www/」去掉就ok啦

讀取資源檔案

資源檔案在存放彈出資訊很有用,以下是讀資源檔案的簡單 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...