The same points as Res/raw and assets:
1. The files in both directories will be stored intact in the APK package after packaging, and will not be compiled into binary.
Different points of Res/raw and assets:
The files in the 1.res/raw are mapped to the R.java file, and the files under the R.id.filename;assets folder are not mapped to R.java when accessed, and Assetmanager classes are required for access.
2.res/raw can not have a directory structure, and assets can have a directory structure, that is, assets directory can be re-established folder
* Read File resources:
1. Read the file resource under the Res/raw, and get the input stream for write operation in the following way
- InputStream is = Getresources (). Openrawresource (R.id.filename);
< Span style= "margin:0px; padding:0px; line-height:1.8 ">2. Read the file resource under assets and get the input stream for write in the following way
- assetmanager am =&NBSP; Span style= "margin:0px; padding:0px; line-height:1.8 ">null ;&NBSP;&NBSP;
- am = Getassets ();
- InputStream is = Am.open ("filename");
The files inside the assets folder are kept in the original file format and need to be read by Assetmanager in the form of a byte stream. 1. First Call getassets () in the activity to get the Assetmanager reference. 2. The Assetmanager open (String fileName, int accessmode) method specifies the read file and the access mode to get the input stream inputstream. 3. Then you read the file with the InputStream of the open file, and remember to InputStream when the read is complete. Close () . 4. Call Assetmanager. Close () closes the Assetmanager.
It is important to note that files from resources and assets can only be read and cannot be written.
The following is read in the assets file:
public class Readasset extends activity{@Overrideprotected void OnCreate (Bundle savedinstancestate) {super. OnCreate (savedinstancestate); Setcontentview (R.layout.read_asset); try {InputStream is = Getassets (). Open ("Read_asset.txt");//a binary file int under a folder Size = Is.available (); Read the entire asset into a local byte buffer. byte[] buffer = new Byte[size]; Is.read (buffer); Is.close (); Convert the buffer into a string. String text = new string (buffer); Finally stick the string into the text view. TextView TV = (TextView) Findviewbyid (R.id.text); Tv.settext (text); } catch (IOException e) {//should never happen! throw new RuntimeException (e); } }}The following is read from the raw file:
Public String Getfromraw () { try { InputStreamReader inputreader = new InputStreamReader (Getresources (). Openrawresource (R.raw.test1)); BufferedReader bufreader = new BufferedReader (inputreader); String line= ""; String result= ""; while (line = Bufreader.readline ()) = null) Result + = line; return Result; } catch (Exception e) { e.printstacktrace (); }