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