Internal storage of Android learning notes data reading and writing of internship data

Source: Internet
Author: User


(1) directory structure


(2) Layout file:

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: Context= ". Mainactivity "> <edittext android:id=" @+id/edittext1 "android:layout_width=" Wrap_content "an droid:layout_height= "Wrap_content" android:layout_alignparentleft= "true" android:layout_alignparenttop= "true "Android:layout_marginleft=" 28DP "android:layout_margintop=" 17DP "android:ems=" ten "/> <butt         On android:id= "@+id/button1" android:layout_width= "wrap_content" android:layout_height= "Wrap_content"   android:layout_alignleft= "@+id/edittext1"     android:layout_below= "@+id/edittext1" android:layout_margintop= "54DP" android:text= "Save Information"/></rel Ativelayout>

(3) The tool class for saving data and reading data: Fileservice.java

Package Com.example.data_storage_interal.file;import Java.io.bytearrayoutputstream;import Java.io.FileInputStream ; Import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import android.content.context;/** * Use internal storage, you can make the application uninstall the time to clear all the relevant information * @author Piaodangdehun * */public class Fileservice { Private context Context;public Fileservice (context context) {This.context = context;} /** * Save content to file * @param filename * @param mode * @param data buffer * @return returns the true and False value */public Boolean Savecontenttof Ile (String fileName, int mode, byte[] data) {Boolean flag = false; FileOutputStream OutputStream = null;try {outputstream = Context.openfileoutput (fileName, mode); try { Outputstream.write (data, 0, data.length);} catch (IOException e) {e.printstacktrace ();} Flag = true;} catch (FileNotFoundException e) {e.printstacktrace ();} finally {if (outputstream! = null) {try {outputstream.close ();} CA TCH (Exception E2) {}}}return true;} /** * Read Data * @param fileName * @return */public string Readcontentfromfile (String fileName) {string result = ""; FileInputStream fileinputstream = null; Bytearrayoutputstream outputstream = new Bytearrayoutputstream (); try {FileInputStream = Context.openfileinput ( fileName); int len = 0;byte[] data = new Byte[1024];while (len = fileinputstream.read (data))! =-1) {Outputstream.write (da TA, 0, Len);} return new String (Outputstream.tobytearray ());} catch (Exception e) {e.printstacktrace ();} Return "";}}

(4) Mainactivity.java

Package Com.example.data_storage_interal;import Com.example.data_storage_interal.file.fileservice;import Android.os.bundle;import Android.app.activity;import Android.content.context;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 button button; Private EditText edittext;private fileservice fileservice; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); button = (Button) This.findviewbyid ( R.id.button1) EditText = (editText) This.findviewbyid (R.ID.EDITTEXT1); fileservice = new Fileservice (this);/* * When the button is clicked, it is stored in the file */button.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {String valuestring = Edittext.gettext (). toString (). Trim (); Boolean flag = Fileservice.savecontenttofile ("Bb.txt", Context.mode_appenD, Valuestring.getbytes ()); if (flag) {Toast.maketext (Mainactivity.this, "saved successfully! ", Toast.length_short). Show ();}});} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}

(5) Test class (to add the appropriate information in the resource file, do not say here)

Package Com.example.data_storage_interal;import Com.example.data_storage_interal.file.fileservice;import Android.app.service;import Android.content.context;import Android.test.androidtestcase;import Android.util.Log; public class MyTest extends Androidtestcase {private static final String TAG = "MyTest";/* * Save data */public void Save () {Fi Leservice fileservice = new Fileservice (GetContext ()), Boolean flag = Fileservice.savecontenttofile ("Aa.txt", Context.mode_private + context.mode_append, "Nihao". GetBytes ()); LOG.I (TAG, "--->>" + flag);} /* * Save data */public void Read () {Fileservice fileservice = new Fileservice (GetContext ()); String msg = fileservice.readcontentfromfile ("Bb.txt"); LOG.I (TAG, "--->>" + msg);}}


After the save is successful, the corresponding file will be found in the DATA-DATA-APK installation directory:



Internal storage of Android learning notes data reading and writing of internship data

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.