学习笔记(四)SharedPreferences与文件管理
来源:互联网 发布:红顶位面商人知轩 编辑:程序博客网 时间:2024/06/06 18:27
Android系统提供了三种方式来实现数据持久化,即android设备在关机的情况的下,这些数据仍然不会丢失。分别为:文件存储,SharedPreference存储以及数据库存储。先说前两个吧:
SharedPreference存储
SharedPreference是使用键值对的方式来存储数据,就是当保存一条数据时,需要给数据提供一个对应的键,这样在读取数据的时候可以通过键把相应的值取出来。
将数据存储到SharedPreference
在存储数据之前,我们首先要通过Context类中的getSharedPreference()方法获得一个SharedPreference对象。
Android的源码:
public abstract SharedPreferences getSharedPreferences(String name,int mode);
此方法有两个参数,第一个是SharedProference文件的名称,如果没有,系统会自动创建;第二个是指定操作的模式,有两种操作模式,默认是:MODE_PRIVATE或者直接写0;MODE_MULTI_PROCESS:一般适用于多个进程对同一个SharedPreference文件进行读写。
存储数据的步骤:
- 调用SharedPreference对象的edit()方法来获取一个SharedPreferences.Editor对象
- 向SharedPreferences.Editor对象中添加数据;
- SharedPreferences.Editor对象调用commit()方法将数据提交,从而完成数据操作 。
从SharedPreferences中读取数据
读取数据会更加简单,直接用SharedPreferences对象的get方法对存储数据进行读取,每种put方法都应一种get方法;
get方法要接收两个参数,第一个参数是键,第二个参数是默认值,找不到相应数据时会以默认值进行返回。
eg(小例子):实现记住密码的功能
首先创建一个布局文件:layout_remember_password.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TableLayout android:layout_width="match_parent" android:layout_height="match_parent" android:stretchColumns="1"> <!--android:stretchColumns="1":表示tablayout的第二列设为可扩展,如果列数没有布满整个屏幕, 则其余空间都被第二列占据,自动拉伸已填充屏幕,就像fill_parent一样。--><TableRow> <TextView android:layout_height="wrap_content" android:text="Account" /> <EditText android:id="@+id/account" android:layout_height="wrap_content" android:hint="Input your account"/> </TableRow> <TableRow> <TextView android:layout_height="wrap_content" android:text="Password"/> <EditText android:id="@+id/password" android:layout_height="wrap_content" android:inputType="textPassword"/> </TableRow> <TableRow> <CheckBox android:id="@+id/remember_password" android:layout_height="wrap_content"/> <TextView android:layout_height="wrap_content" android:text="Remember Password"/> </TableRow> <TableRow> <Button android:id="@+id/login" android:layout_height="wrap_content" android:layout_span="2" android:text="Login"/> </TableRow> </TableLayout> </LinearLayout>
其次创建一个acitivity:RememberPassword_Activity.java
public class RememberPassword_Activity extends Activity{ public static final String REMEMBER_PASSWORD = "Remember_Password"; private SharedPreferences sharedPreferences; private SharedPreferences.Editor editor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_remember_password); sharedPreferences = RememberPassword_Activity.this.getSharedPreferences(REMEMBER_PASSWORD,MODE_PRIVATE); final EditText account= (EditText) findViewById(R.id.account); final EditText password= (EditText) findViewById(R.id.password); final CheckBox checkBox= (CheckBox) findViewById(R.id.remember_password); Button btn_login= (Button) findViewById(R.id.login); Boolean isremember=sharedPreferences.getBoolean("Remember_State",false); if(isremember){ String input_account=sharedPreferences.getString("account",""); String input_password=sharedPreferences.getString("password",""); account.setText(input_account); password.setText(input_password); checkBox.setChecked(true); } btn_login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String input_account=account.getText().toString(); String input_password=password.getText().toString(); if(input_account.equals("one")&&input_password.equals("123")){ editor=sharedPreferences.edit(); if(checkBox.isChecked()) { editor.putString("account", input_account); editor.putString("password", input_password); editor.putBoolean("Remember_State", true); } else editor.clear(); if(editor.commit()) Toast.makeText(RememberPassword_Activity.this,"保存成功",Toast.LENGTH_SHORT).show(); else Toast.makeText(RememberPassword_Activity.this,"保存失败",Toast.LENGTH_SHORT).show(); } else Toast.makeText(RememberPassword_Activity.this,"账号或密码错误",Toast.LENGTH_SHORT).show(); } }); }}
然后运行如图:
文件存储
文件存储是最基本的一种数据存储方式,不对存储内容进行任何形式的修改,原封不动地保存到文件中,比较适合用户存储一些简单的文本数据或二进制数据。
将数据存储到文件中
Context类中提供了openFileOutput()方法,可以将数据存储到指定的文件中,需要接收两个参数,第一个参数是文件名,即文件创建时使用的名称,第二个参数是文件的操作模式,分为MODE_PRIVATE和MODE_APPEND;前者是默认的操作模式,当文件名重名时,写入的内容会覆盖源文件的内容。后者是文件已存在时往文件里面追加内容,不存在创建新的文件。
openFileOutput()方法返回一个FileOutputStream对象,得到返回对象后可以使用Java流的方式将数据写入文件中。
一个简单的保存事例:
private void save() {try {//借助java的文件流,可以向文件写入数据 FileOutputStream fileOutputStream=openFileOutput("test.txt",Context.MODE_PRIVATE); fileOutputStream.write(str.getBytes()); fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } }
从文件中读取数据
Context类中提供了openFileInput()方法,用于从文件中读取数据,只接收一个参数,即要读取的文件名,返回一个FileInputStream对象,也是通过Java流将相应的数据读取出来。
一个简单的读取方法:
private void readFileDemo() { //相关文件读取的操作 try { FileInputStream fileInputStream=openFileInput("test.txt"); BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(fileInputStream)); String test=""; while ((test=bufferedReader.readLine())!=null){ Toast.makeText(MainActivity.this,test,Toast.LENGTH_SHORT).show(); } } catch (Exception e){ e.printStackTrace(); } }
结果如图:
注意:文件存储的所有的文件都是默认存储到/data/data/< package name>/files/目录下。
当然还有读取asset,rows等资源目录下的文件操作,有兴趣的自己了解一下吧!
- 学习笔记(四)SharedPreferences与文件管理
- 第五章 SharedPreferences与文件管理
- Linux学习笔记(四)文件的压缩与打包
- 学习笔记(四)集合、文件与函数
- Linux学习笔记2(文件与目录管理)
- linux学习笔记:文件与目录管理
- linux学习笔记 - 文件与目录管理
- Linux学习笔记(四)-存储管理与文件系统
- Linux&C语言文件学习笔记(四):文件I/O与系统API续
- 学习Linux笔记(四)--文件操作
- Android(OPhone) 学习笔记 - SharedPreferences
- android学习笔记(八)SharedPreferences、editor
- android学习笔记(八)SharedPreferences、editor
- 学习笔记(一) SharedPreferences知识整理
- android学习笔记(八)SharedPreferences、editor
- Android学习笔记(SharedPreferences的使用)
- android学习笔记(八)SharedPreferences、editor .
- Sharedpreferences学习笔记
- HDU 3652 裸数位DP
- 论表单设计规范
- Advanced Fruits
- JAVA学习代码——数组排序
- 实数
- 学习笔记(四)SharedPreferences与文件管理
- 使用Cocos2d时关联源码问题解决
- 分布式存储基本概念
- C++ Primer 第17章 知识点回顾
- 谍影重重5 百度云/迅雷/BT种子1080P高清版下载
- java中String的粗浅使用
- 分类的线性回归方法
- 整数
- 4-1 单链表逆转 (20分)