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