Android data storage-file storage

Source: Internet
Author: User

There are five ways to store data on the Android platform:
1. File storage data;
2. Use sharedpreferences to store data;
3. SQLite database stores data;
4. Use contentprovider to store data;
5. network storage data;
Here we will introduce file storage:
The file storage method is a commonly used method. It reads/writes files in Android and implements I/OProgramIs exactly the same,
Openfileinput () and openfileoutput () methods are provided to read files on the device. In this way, data is stored in the Files folder under data/<package name>.
Of course, files can also be read from the raw folder or asset folder of the program.



 


Import Java. io. fileinputstream; <br/> Import Java. io. fileoutputstream; <br/> Import Java. io. inputstream; </P> <p> Import Org. apache. HTTP. util. encodingutils; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import android. widget. textview; </P> <p> public class fileactivity E Xtends activity {<br/> Public static final string encoding = "UTF-8"; // encoding format <br/> string filename = "test.txt "; // file name <br/> string message = "this is an example of file I/O. "; // Data information written and read <br/> textview TV; <br/> button btn1; <br/> button btn2; <br/> button btn3; <br/>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); <br/> TV = (textview) findviewbyid (R. id. TV); <br/> btn1 = (button) findviewbyid (R. id. btn1); <br/> btn2 = (button) findviewbyid (R. id. btn2); <br/> btn3 = (button) findviewbyid (R. id. btn3); <br/> writefiledata (filename, message); <br/> btn1.setonclicklistener (New onclicklistener () {</P> <p> @ override <br/> Public void onclick (view v) {<br/> // todo auto-generated method stub <br/> TV. settext (readfiledata (filename); <br/>}< br/>}); <br/> btn2.setonclicklistener (New onclicklistener () {</P> <p> @ override <br/> Public void onclick (view v) {<br/> // todo auto-generated method stub <br/> TV. settext (getfromrow ("test1.txt"); </P> <p >}< br/>}); <br/> btn3.setonclicklistener (New onclicklistener () {</P> <p> @ override <br/> Public void onclick (view v) {<br/> // todo auto-generated method stub <br/> TV. settext (getfromasset ("test2.txt"); <br/>}< br/> }); </P> <p >}< br/> // write the specified data to the specified file <br/> Public void writefiledata (string filename, string message) {<br/> try {<br/> fileoutputstream fout = openfileoutput (filename, mode_private); <br/> byte [] bytes = message. getbytes (); // convert the string to be written to a byte array <br/> fout. write (bytes); // write the byte array to the file <br/> fout. close (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> // open a specified file and read its data, returns a String object <br/> Public String readfiledata (string filename) {<br/> string result = ""; <br/> try {<br/> fileinputstream fin = openfileinput (filename); // obtain the fileinputstream object <br/> int length = fin. available (); // get the object length <br/> byte [] buffer = new byte [length]; <br/> fin. read (buffer); <br/> result = encodingutils. getstring (buffer, encoding); // converts a byte array to a string in the specified format. <br/> fin. close (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> return result; <br/>}</P> <p> // method: get the file from the raw folder in resource and read the data <br/> Public String getfromrow (string filename) {<br/> string result = ""; <br/> try {<br/> inputstream in = getresources (). openrawresource (R. raw. test1); <br/> int length = in. available (); <br/> byte [] buffer = new byte [length]; <br/> in. read (buffer); <br/> result = encodingutils. getstring (buffer, encoding); <br/> in. close (); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> return result; <br/>}< br/> // obtain the file from the asset and read the data <br/> Public String getfromasset (string filename) {<br/> string result = ""; <br/> try {<br/> inputstream in = getresources (). getassets (). open (filename); <br/> int length = in. available (); // get the number of bytes of the object <br/> byte [] buffer = new byte [length]; // create a byte array <br/> in. read (buffer); // read the data in the file to the byte array <br/> result = encodingutils. getstring (buffer, encoding); <br/>} catch (exception e) {<br/> E. printstacktrace (); <br/>}< br/> return result; <br/>}< br/>


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.