Files stored in RES/RAW are not compiled by the platform, but are used as available raw resources.
It is very easy to read original resources.
First, call context. getresource to obtain the resources reference of the current application context.
Call openrawresource (int id) to obtain inputstream.
Finally, operate inputstream to get data.
Note: Put the file in the Res/raw directory, the R class will automatically provide this ID.
Speed-up File Reading
The principle is to read some data in the file to the buffer. The advantage is that if the read content is already in the buffer, the buffered data is read.
If no, the buffer reads data from the file first and then the data is read from the buffer. This reduces the number of file operations to improve performance.
The disadvantage is that extra memory is used as the buffer.
The sample code is as follows:
Inputstream is = resources. openrawresource (R. Raw. hubin );
Bufferedinputstream Buf = new bufferedinputstream (is );
Example 1:
Void readrawfile ()
{
String content;
Resources resources = This. getresources ();
Inputstream is = NULL;
Try {
Is = resources. openrawresource (R. Raw. hubin );
Byte buffer [] = new byte [is. Available ()];
Is. Read (buffer );
Content = new string (buffer );
Log. I (TAG, "read:" + content );
}
Catch (ioexception E)
{
Log. E (TAG, "Write File", e );
}
Finally
{
If (is! = NULL)
{
Try {
Is. Close ();
} Catch (ioexception E)
{
Log. E (TAG, "Close file", e );
}
}
}
}