android reads a file stream from a resource file and displays it

  • 2020-06-23 01:57:24
  • OfStack

This article example shows how android reads a file stream from a resource file and displays it. Share to everybody for everybody reference. The details are as follows:

In android, if a text file, such as TXT, is placed under raw, it can be read directly and displayed on the screen like this:


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();
}
// Is to use this.getResources().openRawResource This will do 

Hopefully, this article has been helpful in your Android programming.


Related articles: