Android——数据存储(课堂代码整理:SharedPreferences存储和手机内部文件存储)

layout文件:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hanqi.testapp3.MainActivity"
android:orientation="vertical"> <TextView
android:layout_width="wrap_content"
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存储"
android:onClick="bt_onClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SP读取"
android:onClick="bt1_onClick"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入……"
android:id="@+id/et_1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="写内部文件"
android:onClick="bt2_onClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读内部文件"
android:onClick="bt3_onClick"/>
</LinearLayout>

java类:

 package com.hanqi.testapp3;

 import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintStream; public class MainActivity extends AppCompatActivity { EditText et_1;
TextView tv_1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_1 = (EditText)findViewById(R.id.et_1);
tv_1 = (TextView)findViewById(R.id.tv_1);
}
public void bt_onClick(View v) //SharedPreferences存储
{
//1.得到SharedPreferences对象
SharedPreferences sharedPreferences = getSharedPreferences("abc", MODE_APPEND);
//2.得到编辑器
SharedPreferences.Editor editor =sharedPreferences.edit();
//3.使用Editor添加数据
// editor.putString("a","abcdef");
// editor.putLong("long",12345);
editor.remove("a");
//4.提交保存
editor.commit();
Toast.makeText(MainActivity.this, "保存数据成功", Toast.LENGTH_SHORT).show();
}
//读取
public void bt1_onClick(View v)
{
SharedPreferences sp = getSharedPreferences("abc",MODE_PRIVATE);
String str = sp.getString("a", "默认值");
Toast.makeText(MainActivity.this, "key = a"+" value = "+str, Toast.LENGTH_SHORT).show();
}
//写内部文件(手机内部文件存储)
public void bt2_onClick(View v)
{
//从内存里写入文件
//1.得到内部的存储目录
try {
File file = getFilesDir();
String path = file.getAbsolutePath();
Toast.makeText(MainActivity.this, "path = "+path, Toast.LENGTH_SHORT).show();
//2.用输出流写入文件
FileOutputStream fos = openFileOutput("test.txt",MODE_APPEND);
//3.写入文件内容
PrintStream ps = new PrintStream(fos); String str = et_1.getText().toString(); ps.print(str);
// ps.print("测试");
// ps.println("自动换行");
ps.close(); fos.close();
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
} }
public void bt3_onClick(View v)
{
try {
//输入流
FileInputStream fis = openFileInput("test.txt");
//1.定义byte[]
byte[] b = new byte[1024];
int i = 0;//读到的数据长度
String str1 = "";
//2.循环读
while ((i = fis.read(b))>0)
{
String str = new String(b,0,i);
str1 += str;
}
fis.close();
tv_1.setText(str1);
}
catch (Exception e)
{ }
}
}
上一篇:【RF库XML测试】测试的XML文件说明


下一篇:WPF 触发器