"Advanced Android" how to use data files to save programs

Source: Internet
Author: User

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

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.