Android reads files from resources and assets.

Source: Internet
Author: User

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 ">
  1. PackageXiaohang. zhimeng;
  2. ImportJava. Io. inputstream;
  3. ImportOrg. Apache. http. util. encodingutils;
  4. ImportAndroid. App. activity;
  5. ImportAndroid. Graphics. color;
  6. ImportAndroid. OS. Bundle;
  7. ImportAndroid. widget. textview;
  8. Public ClassActivity02ExtendsActivity {
  9. Public Static FinalString encoding = "UTF-8 ";
  10. Textview TV1;
  11. Textview TV2;
  12. @ Override
  13. Protected VoidOncreate (bundle savedinstancestate ){
  14. Super. Oncreate (savedinstancestate );
  15. Setcontentview (R. layout. Main );
  16. TV1 = (textview) findviewbyid (R. Id. TV1 );
  17. Tv1.settextcolor (color. Red );
  18. Tv1.settextsize (15.0f );
  19. TV2 = (textview) findviewbyid (R. Id. TV2 );
  20. Tv2.settextcolor (color. Red );
  21. Tv2.settextsize (15.0f );
  22. Tv1.settext (getfromraw ());
  23. Tv2.settext (getfromassets ("test2.txt "));
  24. }
  25. // Obtain the file from the raw folder in resources and read the data
  26. PublicString getfromraw (){
  27. String result = "";
  28. Try{
  29. Inputstream in = getresources (). openrawresource (R. Raw. test1 );
  30. // Obtain the object Byte Count
  31. IntLenght = in. Available ();
  32. // Create a byte array
  33. Byte[] Buffer =New Byte[Lenght];
  34. // Read the data in the file to the byte array
  35. In. Read (buffer );
  36. Result = encodingutils. getstring (buffer, encoding );
  37. }Catch(Exception e ){
  38. E. printstacktrace ();
  39. }
  40. ReturnResult;
  41. }
  42. // Obtain the file from the assets folder and read the data
  43. PublicString getfromassets (string filename ){
  44. String result = "";
  45. Try{
  46. Inputstream in = getresources (). getassets (). Open (filename );
  47. // Obtain the object Byte Count
  48. IntLenght = in. Available ();
  49. // Create a byte array
  50. Byte[] Buffer =New Byte[Lenght];
  51. // Read the data in the file to the byte array
  52. In. Read (buffer );
  53. Result = encodingutils. getstring (buffer, encoding );
  54. }Catch(Exception e ){
  55. E. printstacktrace ();
  56. }
  57. ReturnResult;
  58. }
  59. }

Transfer http://byandby.javaeye.com/blog/835744

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.