In the program, there are many ways to store and retrieve data, this article, which describes how to use the file system to save data programming and read operations
I just wrote a helper class that writes and reads files.
/** * For saving program data in a file * * @author Zhaokaiqiang * */public class Filehelper {private static final String TAG = "Filehelper"; Private Context Mcontext; Filehelper (Context _mcontext) {mcontext = _mcontext;} Save information on your phone's local hard drive public void Save (string fileName, string content) {FileOutputStream FileOutputStream = null;try {FILEOUTPU TStream = Mcontext.openfileoutput (filename,context.mode_private); Fileoutputstream.write (Content.getbytes ());} catch (FileNotFoundException e) {e.printstacktrace ()} catch (IOException e) {e.printstacktrace ();} finally {try {if (fil Eoutputstream! = null) {Fileoutputstream.close ();}} catch (IOException e) {e.printstacktrace ();}}} Read the file saved on your phone's hard drive public void read (String fileName) {FileInputStream FileInputStream = null;try {FileInputStream = Mcontext. Openfileinput (fileName); int len = 0;byte[] buffer = new byte[1024]; Bytearrayoutputstream Bytearrayinputstream = new Bytearrayoutputstream (); while (len = fileinputstream.read (buffer))! =-1) {bytearrayinputstream.write (buffer, 0, Len);} String string = new String (Bytearrayinputstream.tobytearray ()); LOG.D (TAG, string);} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} finally {if ( FileInputStream! = null) {try {fileinputstream.close ();} catch (IOException e) {e.printstacktrace ()}}}}
Note: When using write operations. The content that is written overwrites the last written content
The files written are saved under the/data/data/package Name/files folder and can be viewed using DDMS
For example, as seen in:
Export the file using DDMS. You can view the content
These are the data written to our phone's own storage space, false assumptions written to our sdcard, then what should be done?
The following operations are written to SDcard
Save infomation in the Sdcardpublic boolean Savetosdcard (String fileName, string content) {//Judge weather the SDcard Exits,and can be read and writtenif (! Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {return false;} FileOutputStream fileoutputstream = null; File File = new file (Environment.getexternalstoragedirectory (), fileName); try {fileoutputstream = new FileOutputStream ( file); Fileoutputstream.write (Content.getbytes ()); return true;} catch (FileNotFoundException e) {e.printstacktrace ()} catch (IOException e) {e.printstacktrace ();} finally {try {if (fil Eoutputstream! = null) {Fileoutputstream.close ();}} catch (IOException e) {e.printstacktrace ();}} return false;}
Here's how to read a file located under the SDcard root folder
Read the file in the Sdcardpublic string Readfromsd (String fileName) {FileInputStream fileinputstream = null; File File = new file (Environment.getexternalstoragedirectory (), fileName); try {fileinputstream = new FileInputStream ( file); int len = 0;byte[] buffer = new byte[1024]; Bytearrayoutputstream Bytearrayinputstream = new Bytearrayoutputstream (); while (len = fileinputstream.read (buffer))! =-1) {bytearrayinputstream.write (buffer, 0, Len);} String string = new String (Bytearrayinputstream.tobytearray ()); return string;} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} finally {if ( FileInputStream! = null) {try {fileinputstream.close ();} catch (IOException e) {e.printstacktrace ()}}} return null;}
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
"Advanced Android" how to use data files to save programs