File storage is used in Android development, so learn today.
Change the layout interface first:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Android:layout_width= "Match_parent"4 Android:layout_height= "Match_parent"5 android:orientation= "vertical" >6 7 <TextView8 Android:id= "@+id/filename"9 Android:layout_width= "Wrap_content"Ten Android:layout_height= "20DP" One android:textsize= "15DP" A Android:text= "File name" /> - - <EditText the Android:id= "@+id/edit_name" - Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content"/> - + <TextView - Android:id= "@+id/filecontent" + Android:layout_width= "Wrap_content" A Android:layout_height= "20DP" at android:textsize= "15DP" - Android:text= "File contents" /> - - <EditText - Android:id= "@+id/edit_content" - Android:layout_width= "Match_parent" in Android:layout_height= "Wrap_content"/> - to <LinearLayout + Android:layout_width= "Match_parent" - Android:layout_height= "Match_parent" the android:orientation= "Horizontal" > * $ <ButtonPanax Notoginseng Android:id= "@+id/btn_save" - Android:layout_width= "Wrap_content" the Android:layout_height= "Wrap_content" + Android:text= "Save File" /> A the <Button + Android:id= "@+id/btn_read" - Android:layout_width= "Wrap_content" $ Android:layout_height= "Wrap_content" $ Android:text= "Read File" /> - </LinearLayout> - the </LinearLayout>
Then change the mainactivity file:
1 PackageCom.example.filesave;2 3 ImportJava.io.ByteArrayOutputStream;4 ImportJava.io.FileInputStream;5 ImportJava.io.FileOutputStream;6 7 Importandroid.app.Activity;8 ImportAndroid.content.Context;9 ImportAndroid.os.Bundle;Ten ImportAndroid.view.View; One ImportAndroid.widget.Button; A ImportAndroid.widget.EditText; - - Public classMainactivityextendsActivity { the - PrivateContext context = This; - PrivateButton btnsave =NULL; - PrivateButton Btnread =NULL; + PrivateEditText Editname =NULL; - PrivateEditText editcontent =NULL; + A @Override at protected voidonCreate (Bundle savedinstancestate) { - Super. OnCreate (savedinstancestate); - Setcontentview (r.layout.activity_main); - -Btnsave =(Button) Findviewbyid (r.id.btn_save); -Btnread =(Button) Findviewbyid (r.id.btn_read); inEditname =(EditText) Findviewbyid (r.id.edit_name); -Editcontent =(EditText) Findviewbyid (r.id.edit_content); to +Btnsave.setonclicklistener (NewView.onclicklistener () { - the @Override * Public voidOnClick (View v) { $ Panax NotoginsengString filename =Editname.gettext (). toString (); -String filecontent =Editcontent.gettext (). toString (); theFileOutputStream out =NULL; + Try { Aout =Context the . Openfileoutput (filename, context.mode_private); +Out.write (Filecontent.getbytes ("UTF-8")); - $Editname.settext ("Already saved name!")); $Editcontent.settext ("Saved content!")); - -}Catch(Exception e) { the e.printstacktrace (); -}finally {Wuyi Try { the out.close (); -}Catch(Exception e) { Wu e.printstacktrace (); - } About } $ } - }); - -Btnread.setonclicklistener (NewView.onclicklistener () { A + @Override the Public voidOnClick (View v) { -String filename = Editname.gettext (). toString ();//get the name of the file read $FileInputStream in =NULL; theBytearrayoutputstream bout =NULL; the byte[] buf =New byte[1024]; thebout =NewBytearrayoutputstream (); the intLength = 0; - Try { inin = Context.openfileinput (filename);//Get input stream the while(length = In.read (BUF))! =-1) { theBout.write (buf, 0, length); About } the byte[] content =Bout.tobytearray (); theEditcontent.settext (NewString (Content, "UTF-8"));//set the contents of a text box for reading the}Catch(Exception e) { + e.printstacktrace (); - } theEditcontent.invalidate ();//Refresh ScreenBayi Try { the in.close (); the bout.close (); -}Catch(Exception e) { - } the } the the the }); - the } the the}
Operating effect:
Enter the file name and file content separately as "AAAA", "1234",
After clicking Save file, the interface becomes:
And then modify the file name called "AAAA",
Finally click the Read File button, the file content is displayed as "1234", test success.
19.Android of file storage method learning