Android Reads TXT file contents

Source: Internet
Author: User

Android Res folder is used to store resources, you can create a raw folder under the Res folder, the content placed in the raw folder will be packaged as is, and will not be compiled into binary files, and can be easily accessed through the R file. For example, we can put the update information, copyright information, etc. into the TXT file, and then put in the raw file, and then easy to access.

Put a a.txt file in raw and you can use Getresources () in activity. Openrawresource (R.RAW.A); method gets a InputStream class for this file, You can then easily read and write A.txt.
1     InputStream InputStream = Getresources (). Openrawresource (R.RAW.A);
View Code

A way to get the contents of a string in InputStream: Pass in a inputstream and return the text content.

1      Public StaticString getString (InputStream inputstream) {2InputStreamReader InputStreamReader =NULL;3         Try {4InputStreamReader =NewInputStreamReader (InputStream,"GBK");5}Catch(unsupportedencodingexception E1) {6 e1.printstacktrace ();7         }8BufferedReader reader =NewBufferedReader (inputstreamreader);9StringBuffer SB =NewStringBuffer ("");Ten String Line; One         Try { A              while(line = Reader.readline ())! =NULL) { - Sb.append (line); -Sb.append ("\ n"); the             } -}Catch(IOException e) { - e.printstacktrace (); -         } +         returnsb.tostring (); -}
View Code

Read content in GBK encoding, different text files may be encoded differently, if garbled, you may need to adjust the encoding.

1 New " GBK ");
View Code

Android Reads TXT file contents

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.