On the Android platform, in addition to operating files in the private folder of the application, you can also obtain the input stream to read data from resource files and assets, these files are stored in the Res/raw directory and Assets Directory of the application. These files are packaged together with other files during compilation.
Note that files from resources and assets can only be read but cannot be written. The following example shows how to read information from files in resources and assets. First, create two text files "test1.txt" and "test2.txt" in the Res/raw and assets directories for reading. The structure is as follows.
To avoid the hassle of string transcoding, you can set the encoding formats of two text files to UTF-8. There are many ways to set the encoding format, a simple one is to open a text file with Windows notepad, In the Save As dialog box encoding format select "UTF-8", such.
Check the running result.
Let's take a look at the code.
Activity02
Java code {
Function onclick ()
{
DP. Sh. toolbar. copytoclipboard (this); Return false;
}
} "Href =" http://byandby.javaeye.com/blog/835744 ">
- PackageXiaohang. zhimeng;
- ImportJava. Io. inputstream;
- ImportOrg. Apache. http. util. encodingutils;
- ImportAndroid. App. activity;
- ImportAndroid. Graphics. color;
- ImportAndroid. OS. Bundle;
- ImportAndroid. widget. textview;
- Public ClassActivity02ExtendsActivity {
- Public Static FinalString encoding = "UTF-8 ";
- Textview TV1;
- Textview TV2;
- @ Override
- Protected VoidOncreate (bundle savedinstancestate ){
- Super. Oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- TV1 = (textview) findviewbyid (R. Id. TV1 );
- Tv1.settextcolor (color. Red );
- Tv1.settextsize (15.0f );
- TV2 = (textview) findviewbyid (R. Id. TV2 );
- Tv2.settextcolor (color. Red );
- Tv2.settextsize (15.0f );
- Tv1.settext (getfromraw ());
- Tv2.settext (getfromassets ("test2.txt "));
- }
- // Obtain the file from the raw folder in resources and read the data
- PublicString getfromraw (){
- String result = "";
- Try{
- Inputstream in = getresources (). openrawresource (R. Raw. test1 );
- // Obtain the object Byte Count
- IntLenght = 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. printstacktrace ();
- }
- ReturnResult;
- }
- // Obtain the file from the assets folder and read the data
- PublicString getfromassets (string filename ){
- String result = "";
- Try{
- Inputstream in = getresources (). getassets (). Open (filename );
- // Obtain the object Byte Count
- IntLenght = 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. printstacktrace ();
- }
- ReturnResult;
- }
- }
Transfer http://byandby.javaeye.com/blog/835744