Layout file:
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"3 Xmlns:tools= "Http://schemas.android.com/tools"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Match_parent"6 Android:paddingbottom= "@dimen/activity_vertical_margin"7 Android:paddingleft= "@dimen/activity_horizontal_margin"8 Android:paddingright= "@dimen/activity_horizontal_margin"9 Android:paddingtop= "@dimen/activity_vertical_margin"Ten Tools:context= "Com.hanqi.testapp3.MainActivity" One android:orientation= "vertical"> A - <TextView - Android:layout_width= "Wrap_content" the Android:layout_height= "Wrap_content" - Android:text= "Hello world!" - Android:id= "@+id/tv_1"/> - <Button + Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" + Android:text= "SP Storage" A Android:onclick= "Bt_onclick"/> at <Button - Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" - Android:text= "sp read" - Android:onclick= "Bt1_onclick"/> - <EditText in Android:layout_width= "Match_parent" - Android:layout_height= "Wrap_content" to Android:hint= "Please enter ..." + Android:id= "@+id/et_1"/> - <Button the Android:layout_width= "Match_parent" * Android:layout_height= "Wrap_content" $ Android:text= "Write internal file"Panax Notoginseng Android:onclick= "Bt2_onclick"/> - <Button the Android:layout_width= "Match_parent" + Android:layout_height= "Wrap_content" A Android:text= "Read internal file" the Android:onclick= "Bt3_onclick"/> + </LinearLayout>
Java class:
1 Packagecom.hanqi.testapp3;2 3 Importandroid.content.SharedPreferences;4 ImportAndroid.os.Bundle;5 Importandroid.support.v7.app.AppCompatActivity;6 ImportAndroid.view.View;7 ImportAndroid.widget.EditText;8 ImportAndroid.widget.TextView;9 ImportAndroid.widget.Toast;Ten One ImportJava.io.File; A ImportJava.io.FileInputStream; - ImportJava.io.FileOutputStream; - ImportJava.io.PrintStream; the - Public classMainactivityextendsappcompatactivity { - - EditText et_1; + TextView tv_1; - @Override + protected voidonCreate (Bundle savedinstancestate) { A Super. OnCreate (savedinstancestate); at Setcontentview (r.layout.activity_main); -Et_1 =(EditText) Findviewbyid (r.id.et_1); -Tv_1 =(TextView) Findviewbyid (r.id.tv_1); - } - Public voidBt_onclick (View v) //sharedpreferences storage - { in //1. Get Sharedpreferences Object -Sharedpreferences sharedpreferences = getsharedpreferences ("abc", mode_append); to //2. Get the editor +Sharedpreferences.editor Editor =Sharedpreferences.edit (); - //3. Adding Data Using editor the //editor.putstring ("A", "abcdef"); * //Editor.putlong ("Long", 12345); $Editor.remove ("a");Panax Notoginseng //4. Submit a Save - editor.commit (); theToast.maketext (mainactivity. This, "Save data successfully", Toast.length_short). Show (); + } A //Read the Public voidBt1_onclick (View v) + { -Sharedpreferences sp = getsharedpreferences ("abc", mode_private); $String str = sp.getstring ("A", "Default value")); $Toast.maketext (mainactivity. This, "key = a" + "value =" +str, toast.length_short). Show (); - } - //Write internal files (phone internal file storage) the Public voidBt2_onclick (View v) - {Wuyi //write files from memory the //1. Get the internal storage directory - Try { WuFile File =Getfilesdir (); -String Path =File.getabsolutepath (); AboutToast.maketext (mainactivity. This, "path =" +path, Toast.length_short). Show (); $ //2. Writing to a file with an output stream -FileOutputStream fos = openfileoutput ("Test.txt", mode_append); - //3. Writing file Contents -PrintStream PS =NewPrintStream (FOS); A +String str =Et_1.gettext (). toString (); the - ps.print (str); $ //ps.print ("test"); the //ps.println ("line wrapping"); the ps.close (); the the fos.close (); -Toast.maketext (mainactivity. This, "Saved successfully", Toast.length_short). Show (); in } the Catch(Exception e) the { AboutToast.maketext (mainactivity. This, "Save Failed", Toast.length_short). Show (); the } the the } + Public voidBt3_onclick (View v) - { the Try {Bayi //input Stream theFileInputStream FIS = openfileinput ("Test.txt"); the //1. Definition byte[] - byte[] B =New byte[1024]; - inti = 0;//length of data read theString str1 = ""; the //2. Cyclic reading the while((i = Fis.read (b)) >0) the { -String str =NewString (b,0, i); theSTR1 + =str; the } the fis.close ();94 Tv_1.settext (str1); the } the Catch(Exception e) the {98 About } - }101}
android--data Storage (Classroom code collation: sharedpreferences Storage and mobile internal file storage)