To read the original format file, you must first call the getresource () function to obtain the resource object, and then call the openrawresource () function of the resource object to open the specified original format file in the form of a binary stream. After the file is read, call the close () function to close the file stream.
The core code for reading files in the original format is as follows:
Resources resources = This. getresources ();
Inputstream = NULL;
Try {
Inputstream = resources. openrawresource (R. Raw. raw_file );
Byte [] reader = new byte [inputstream. Available ()];
While (inputstream. Read (Reader )! =-1 ){
}
Self-organized:
View code
1 resources Resources = This. getresources (); // obtain the resource file when the program is initialized.
2 inputstream = NULL; // get the input stream
3 try {
4 inputstream = resources. openrawresource (R. Raw. raw_file); // open the specified original format file in the form of a binary stream
5 byte [] reader = new byte [inputstream. Available ()]; // store the obtained stream data in Reader
6 While (inputstream. Read (Reader )! =-1) {// always read to the end
7}
8 displayview. settext (new string (reader, "UTF-8"); // display in edittext
9} catch (ioexception e ){
10 log. E ("resourcefiledemo", E. getmessage (), e );
11} finally {
12 if (inputstream! = NULL ){
13 try {
14 inputstream. Close (); // close the input stream
15}
16 catch (ioexception e ){}
17}
18}