Assets folder Resource Access All files in the assets folder are in the original file format. You need to use assetmanager to read files in the form of byte streams. 1. Call in activity first
Getassets ()To obtain the assetmanager reference. 2. Use assetmanager's
Open (string filename, int accessmode)The method specifies the file to be read and the access mode to get the input stream inputstream. 3. Use the inputstream of the open file to read the file. After reading the file, remember it as inputstream.
Close (). 4. Call assetmanager.
Close ()Disable assetmanager.
Note that files from resources and assets can only be read but cannot be written.
Read from raw files as follows:
Code
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 ();
}
}
Read from assets directly
Code
Public String getfromassets (string filename ){
Try {
Inputstreamreader inputreader = new inputstreamreader (getresources (). getassets (). Open (filename ));
Bufferedreader bufreader = new bufferedreader (inputreader );
String line = "";
String result = "";
While (line = bufreader. Readline ())! = NULL)
Result + = line;
Return result;
} Catch (exception e ){
E. printstacktrace ();
}
}
Of course, if you want to get the memory stream, you can also directly return the memory stream! Next, create a new project file named assetsdemo.
Create a layout file, as shown below, which is simple. You can see it at a Glance without further introduction.
Afterwards, I searched for the text from the Internet and saved it in the Assets Directory. The name was health.txt. This is the file we want to read today.
To view the. txt file, double-click it. As shown below.
Next, the core code for Android to read files is the highlight of today. Paste the Code directly. package com.assets.cn;
Import Java. io. inputstream;
Import Org. apache. HTTP. util. encodingutils;
Import android. app. activity;
Import android. graphics. color;
Import android. OS. bundle;
Import android. widget. textview;
public class assetsdemoactivity extends activity {
Public static final string encoding = "UTF-8";
textview TV1;
@ override
Public void oncreate (bundle savedinstancestate) {
super. oncreate (savedinstancestate);
setcontentview (R. layout. main);
TV1 = (textview) findviewbyid (R. id. TV1);
tv1.settextcolor (color. black);
tv1.settextsize (25366f);
tv1.settext (getfromassets ("health.txt");
}
// obtain the file from the assets folder and read the data
Public String getfromassets (string filename) {
string result = "";
try {
inputstream in = getresources (). getassets (). open (filename);
// get the number of bytes of the file
int lenght = in. available ();
// create a byte array
byte [] buffer = new byte [lenght];
// read the data in the file to the byte array.
in. read (buffer);
result = encodingutils. getstring (buffer, encoding);
}catch (exception e) {
E. PR Intstacktrace ();
}< br> return result;
}< br>
here is the mainfest file.
package = "com.assets.cn"
Android: versioncode = "1"
Android: versionname = "1.0">
Android: Label =" @ string/app_name ">
</Application>
</Manifest>
Finally, let's run it.Program.
Source code:Http://files.cnblogs.com/greatverve/AssetsDemo.zip
Too busy. Take time to study sgf reading and writing.
URL: http://greatverve.cnblogs.com/archive/2012/03/07/android-assets.html