你的位置:首页 > 软件开发 > 操作系统 > Android笔记——Android中数据的存储方式

Android笔记——Android中数据的存储方式

发布时间:2015-12-08 19:01:02
Android中数据的存储方式1.1 方式Shared Preferences:主要用于保存程序的系统配置信息。用来存储“key-values paires”。一般用于保存程序启动时设定的信息,以便在程序下一次启动时继续保留前一次设定 ...

 

   Android中数据的存储方式

1.1  方式

  1. Shared Preferences:主要用于保存程序的系统配置信息。用来存储“key-values paires”。一般用于保存程序启动时设定的信息,以便在程序下一次启动时继续保留前一次设定的信息。
  2. Files:用文件的形式保存信息。可以通过对文件的读写来获取或保存相关信息。
  3. SQLite:用数据库的形式保存信息。SQLite是一个开源的数据库 系统。
  4. NetWork:将数据保存于网络

1.2  区别

  1.  Shared Preferences:

  Android提供用来存储一些简单的配置信息的一种机制,例如,一些默认欢迎语、登录的用户名和密码等。其以键值对的方式存储。

  SharedPreferences是以

  2.  Files

  在Android中,其提供了openFileInput 和 openFileOuput 方法读取设备上的文件,下面看个例子代码,具体如下所示:

String FILE_NAME = "tempfile.tmp"; //确定要操作文件的文件名FileOutputStream fos = openFileOutput(FILE_NAME, Context.MODE_PRIVATE); //初始化FileInputStream fis = openFileInput(FILE_NAME); //创建写入流
public void login(View v){ String name = et_name.getText().toString(); String pass = et_pass.getText().toString(); CheckBox cb = (CheckBox) findViewById(R.id.cb); if(cb.isChecked()){ //指定Android的内部存储空间的路径// File file = new File("data/data/com.bokeyuan.rwinrom/info.txt"); //返回一个File对象,它的路径是:data/data/com.bokeyuan.rwinrom/files/// File file = new File(getFilesDir(), "info.txt"); //返回一个File对象,它的路径是:data/data/com.bokeyuan.rwinrom/cache/ File file = new File(getCacheDir(), "info.txt"); try { FileOutputStream fos = new FileOutputStream(file); fos.write((name + "##" + pass).getBytes()); fos.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } //弹出提示框,提示用户登录成功 Toast.makeText(this, "登陆成功", 0).show(); } public void readAccount(){ //指定Android的内部存储空间的路径// File file = new File("data/data/com.bokeyuan.rwinrom/info.txt");// File file = new File(getFilesDir(), "info.txt"); File file = new File(getCacheDir(), "info.txt"); if(file.exists()){ try { FileInputStream fis = new FileInputStream(file); //把字节流转换成字符流 BufferedReader br = new BufferedReader(new InputStreamReader(fis)); String text = br.readLine(); String[] s = text.split("##"); et_name.setText(s[0]); et_pass.setText(s[1]); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}

原标题:Android笔记——Android中数据的存储方式

关键词:Android

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录