Android——数据存储(课堂代码整理:SharedPreferences存储和手机内部文件存储)...
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Android——数据存储(课堂代码整理:SharedPreferences存储和手机内部文件存储)...
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                layout文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns: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" 10 tools:context="com.hanqi.testapp3.MainActivity" 11 android:orientation="vertical"> 12 13 <TextView 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:text="Hello World!" 17 android:id="@+id/tv_1"/> 18 <Button 19 android:layout_width="match_parent" 20 android:layout_height="wrap_content" 21 android:text="SP存儲" 22 android:onClick="bt_onClick"/> 23 <Button 24 android:layout_width="match_parent" 25 android:layout_height="wrap_content" 26 android:text="SP讀取" 27 android:onClick="bt1_onClick"/> 28 <EditText 29 android:layout_width="match_parent" 30 android:layout_height="wrap_content" 31 android:hint="請輸入……" 32 android:id="@+id/et_1"/> 33 <Button 34 android:layout_width="match_parent" 35 android:layout_height="wrap_content" 36 android:text="寫內部文件" 37 android:onClick="bt2_onClick"/> 38 <Button 39 android:layout_width="match_parent" 40 android:layout_height="wrap_content" 41 android:text="讀內部文件" 42 android:onClick="bt3_onClick"/> 43 </LinearLayout>java類:
1 package com.hanqi.testapp3; 2 3 import android.content.SharedPreferences; 4 import android.os.Bundle; 5 import android.support.v7.app.AppCompatActivity; 6 import android.view.View; 7 import android.widget.EditText; 8 import android.widget.TextView; 9 import android.widget.Toast; 10 11 import java.io.File; 12 import java.io.FileInputStream; 13 import java.io.FileOutputStream; 14 import java.io.PrintStream; 15 16 public class MainActivity extends AppCompatActivity { 17 18 EditText et_1; 19 TextView tv_1; 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_main); 24 et_1 = (EditText)findViewById(R.id.et_1); 25 tv_1 = (TextView)findViewById(R.id.tv_1); 26 } 27 public void bt_onClick(View v) //SharedPreferences存儲 28 { 29 //1.得到SharedPreferences對象 30 SharedPreferences sharedPreferences = getSharedPreferences("abc", MODE_APPEND); 31 //2.得到編輯器 32 SharedPreferences.Editor editor =sharedPreferences.edit(); 33 //3.使用Editor添加數據 34 // editor.putString("a","abcdef"); 35 // editor.putLong("long",12345); 36 editor.remove("a"); 37 //4.提交保存 38 editor.commit(); 39 Toast.makeText(MainActivity.this, "保存數據成功", Toast.LENGTH_SHORT).show(); 40 } 41 //讀取 42 public void bt1_onClick(View v) 43 { 44 SharedPreferences sp = getSharedPreferences("abc",MODE_PRIVATE); 45 String str = sp.getString("a", "默認值"); 46 Toast.makeText(MainActivity.this, "key = a"+" value = "+str, Toast.LENGTH_SHORT).show(); 47 } 48 //寫內部文件(手機內部文件存儲) 49 public void bt2_onClick(View v) 50 { 51 //從內存里寫入文件 52 //1.得到內部的存儲目錄 53 try { 54 File file = getFilesDir(); 55 String path = file.getAbsolutePath(); 56 Toast.makeText(MainActivity.this, "path = "+path, Toast.LENGTH_SHORT).show(); 57 //2.用輸出流寫入文件 58 FileOutputStream fos = openFileOutput("test.txt",MODE_APPEND); 59 //3.寫入文件內容 60 PrintStream ps = new PrintStream(fos); 61 62 String str = et_1.getText().toString(); 63 64 ps.print(str); 65 // ps.print("測試"); 66 // ps.println("自動換行"); 67 ps.close(); 68 69 fos.close(); 70 Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show(); 71 } 72 catch (Exception e) 73 { 74 Toast.makeText(MainActivity.this, "保存失敗", Toast.LENGTH_SHORT).show(); 75 } 76 77 } 78 public void bt3_onClick(View v) 79 { 80 try { 81 //輸入流 82 FileInputStream fis = openFileInput("test.txt"); 83 //1.定義byte[] 84 byte[] b = new byte[1024]; 85 int i = 0;//讀到的數據長度 86 String str1 = ""; 87 //2.循環讀 88 while ((i = fis.read(b))>0) 89 { 90 String str = new String(b,0,i); 91 str1 += str; 92 } 93 fis.close(); 94 tv_1.setText(str1); 95 } 96 catch (Exception e) 97 { 98 99 } 100 } 101 }?
轉載于:https://www.cnblogs.com/hanazawalove/p/5526838.html
總結
以上是生活随笔為你收集整理的Android——数据存储(课堂代码整理:SharedPreferences存储和手机内部文件存储)...的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: Resource接口,及资源
 - 下一篇: 广西柳州可种什么品种蓝莓?