本文實例講述了android從資源文件中讀取文件流并顯示的方法。分享給大家供大家參考。具體如下:
在android中,假如有的文本文件,比如TXT放在raw下,要直接讀取出來,放到屏幕中顯示,可以這樣:
private void doRaw(){ InputStream is = this.getResources().openRawResource(R.raw.ziliao); try{ doRead(is); }catch(IOException e){ e.printStackTrace(); }}private void doRead(InputStream is) throws IOException{ DataInputStream dis = new DataInputStream(is); byte[]buffer = new byte[is.available()]; dis.readFully(buffer); textView.setText(new String(buffer)); dis.close(); is.close();}//就是用this.getResources().openRawResource這個就可以了
希望本文所述對大家的Android程序設計有所幫助。
新聞熱點
疑難解答
圖片精選