Android data storage and access (using files for data storage)

Source: Internet
Author: User
Tags readfile

Click Save and save the created file in/data/<package name>/Files
Directory, you can check: ddms -- file Explorer -- Data -- <package name> --files--file.txt

Select a file and click it to export the file for viewing. Click it to add other files.

After you click "save", the prompt box is displayed, indicating that the file is saved successfully. After you click "read", the prompt box displays file content: ×××××.

Layout File

Activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="left"        android:text="@string/name" />    <EditText        android:id="@+id/name"        android:layout_width="fill_parent"        android:layout_height="wrap_content" />    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="left"        android:text="@string/content" />    <EditText        android:id="@+id/content"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:inputType="textMultiLine"        android:minLines="5" />    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:orientation="horizontal" >        <Button            android:id="@+id/save"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/save" />        <Button            android:id="@+id/read"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="@string/read" />    </LinearLayout></LinearLayout>

Business Layer Code:

Fileservice. Java

Package CN. BZU. fileoperation; import Java. io. bytearrayoutputstream; import Java. io. fileinputstream; import Java. io. fileoutputstream; import Java. io. ioexception; import android. content. context; public class fileservice {private context; Public fileservice (context) {super (); this. context = context;} // Save Public void save (string filename, string content) throws exception {fileoutputstream Fos = Co Ntext. openfileoutput (filename, context. mode_private); FOS. write (content. getbytes (); FOS. close () ;}// read data Public String readfile (string filename) throws ioexception {fileinputstream FCM = context. openfileinput (filename); int Len = 0; byte [] buffer = new byte [1024]; bytearrayoutputstream baos = new bytearrayoutputstream (); // output data to the memory while (LEN = Fi. read (buffer ))! =-1) {// if the data volume is large, the 2nd read data may overwrite the 1st read data to baos. write (buffer, 0, Len);} byte [] DATA = baos. tobytearray (); // obtain the baos in which data is stored in binary format. close (); FCM. close (); return new string (data); // convert binary data to the corresponding string }}

Mainactivity. Java code:

Package CN. BZU. fileoperation; import Java. io. ioexception; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. edittext; import android. widget. toast; public class mainactivity extends activity {private edittext nametext; private edittext contenttext; private button save; private button read; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); nametext = (edittext) This. findviewbyid (R. id. name); contenttext = (edittext) This. findviewbyid (R. id. content); save = (button) This. findviewbyid (R. id. save); read = (button) This. findviewbyid (R. id. read); Final fileservice = new fileservice (this); save. setonclicklistener (New onclicklistener () {public void onclick (view v) {string name = nametext. gettext (). tostring (); string content = contenttext. gettext (). tostring (); try {fileservice. save (name, content);} catch (exception e) {// todo auto-generated catch blocke. printstacktrace ();} toast. maketext (mainactivity. this, "saved successfully", toast. length_long ). show () ;}}); read. setonclicklistener (New onclicklistener () {string filecontent; @ overridepublic void onclick (view v) {string name = nametext. gettext (). tostring (); try {filecontent = fileservice. readfile (name);} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} toast. maketext (mainactivity. this, "File Content:" + filecontent, toast. length_long ). show () ;}}) ;}@ overridepublic Boolean oncreateoptionsmenu (menu) {getmenuinflater (). inflate (R. menu. activity_main, menu); Return true ;}}

 

 

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.