Android 项目之--数据存储

来源:互联网 发布:如何面试淘宝运营助理 编辑:程序博客网 时间:2024/06/08 08:37

(源自:http://dev.10086.cn/cmdn/bbs/viewthread.php?tid=17932&highlight=)
继上篇数据存储,现在我们来讲讲另外一种数据存储,Files。本篇讲述步骤如下:

  • 1、温故而知新,复习四种数据存储的区别。
  • 2、什么是 Files 数据存储。
  • 3、什么是 Properties ?
  • 4、Properties 重要方法和属性讲解。
  • 5、模拟用户设置参数。
  • 6、查看 Files 产生的文件。

1、温故而知新,复习四种数据存储的区别
Android 总共有4种数据存储方式,具体解释和列表如下:

  • Shared Preferences
    用来存储“键-值”对的格式数据。是一个轻量级的键值对存储机制,只可以存储基本数据类型。
    你可以参考上篇文章了解和使用它的用法:Android 小项目之--数据存储【Shared Preferences】(附源码)
  • Files
    它通过 FileInputStream 和 FileOutputStream 对文件进行操作。本篇将讲述如何使用Files 数据存储。
  • SQLite
    Android 提供的一个标准数据库,支持SQL语句。
    你可以参考这篇文章大致了解和使用它的用法:Android 小项目之--SQLite 使用法门 (附源码)
  • NetWork
    通过网络来存储和获得数据。

2、什么是 Files 数据存储

  • File 就是把需要保存的东西通过文件的形式讯录下来,当需要这些数据时,通过读取这个文件来获取这些数据。因为 Android 采用了 Linux 核心,所以在Android 系统中,文件也是Linux 的形式。
  • Android 中可以在设备本身的的存储或者外接的存储设备中创建用于保存数据的文件。同时,在默认状态下,文件是不能在不同的程序间共享的。

3、什么是 Properties ?
Properties(属性),可以把Properties继承自Hashtable,理解成一个Hashtable ,不过唯一不同的是,Properties对应的“键-值”必须是字符串形式的数据类型。Files 数据存储主要是使用 Properties 配合 FileInputStream或者FileOutputStream对文件写入操作。

4、Properties 重要方法和属性讲解
公用方法:

  • 返回值:String
    方法:getProperty(String name, String defaultValue)
    解释:通过指定的 “name“ 即Key,搜索属性,参数二为默认值,即通过Key找不到文件中的属性时,要返回的默认值。
  • 返回值:String
    方法:getProperty(String name)
    解释:通过指定的 ”name“ 即为 Key,搜索属性,没有返回默认值。
  • 无返回值:void
    方法:list(PrintStream out)
    解释:通过PrintStream 列出可读的属性列表
  • 无返回值:void
    方法:list(PrintWriter writer)
    解释:通过PrintStream 列出可写的属性列表
  • 无返回值:synchronized void
    方法:load(InputStream in)
    解释:从指定的 ”inputStream “ 即输出流,加载properties
  • 无返回值:synchronized void
    方法:loadFromXML(InputStream in)
    解释:从指定的 "InputStream" 即输出流,加载一个以XML形式的 Properties
  • 返回值:Enumeration<?>
    方法:propertyNames()
    解释:返回所有包含在文件里面的属性名称
  • 无返回值:void
    方法:save(OutputStream out, String comment)
    解释:注意,这种保存方法己经过时,Google 不推荐使用此种写法,这种方法忽略任何IO 异常,所以在实际操作过程中,可能会发生不必要的异常。
  • 返回值:object
    方法:setProperty(String name, String value)
    解释:设置属性,保存一个”键-值“对的属性。
  • 无返回值:synchronized void
    方法:store(OutputStream out, String comment)
    解释:通过 FileOutputStream 打开对应的程序文件,然后通过Store 保存之前 Properties 打包好的数据。这里备注可以为空。
  • 无返回值:void
    方法:storeToXML(OutputStream os, String comment)
    解释:通过FileOutputStream 打开对应的程序文件,将打包好的数据写入到XML文件。
  • 无返回值:synchronized void
    方法:storeToXML(OutputStream os, String comment, String encoding)
    解释:通过FileOutputStream 打开对应的程序文件,将打包好的数据写入到XML文件,第三个参数可以指定编码。

5、模拟用户设置参数
本篇还是以上篇例子为基础,还是以保存音乐播放状态来对Properties的使用进行大概的了解。本例中,实现了load方法,即加载用户之前保存的属性文件,然后通过获取对应的KEY值为状态赋值。此外还有一个save方法用来保存用户退出程序时的播放状态。
load方法代码如下:
本篇Load方法代码参考

  1. void load()
  2.     {
  3.         Properties properties=new Properties();
  4.         try {
  5.             FileInputStream stream =this.openFileInput("music.cfg");
  6.             properties.load(stream);
  7.         } catch (FileNotFoundException e) {
  8.             // TODO: handle exception
  9.             return;
  10.         } catch (IOException e) {
  11.             // TODO Auto-generated catch block
  12.             return;
  13.         }
  14.         isplay=Boolean.valueOf(properties.get("isplay").toString());
  15.     }
复制代码

注意:

  • 1、 properties.load方法如果遇到错误将抛出 IOException 异常,并且使用的编码为:ISO8859 - 1
  • 2、加载的数据出现空格将会被忽略。
  • 3、不要在你的Files 文件中加注释 ,因为load的时候将会忽略你的注释符号。
  • 4、公认的转义序列:“/”,“/ /”,“/ r”开始,“/ ñ”,“/!”,“/#”,“/ t”的,“/ b”与“/ f”的,和“/ uXXXX”(Unicode字符)。

save方法代码参考:
Save 方法代码参考

  1. boolean save()
  2.     {
  3.         Properties properties =new Properties();
  4.         properties.put("isplay", String.valueOf(isplay));
  5.         try {
  6.             FileOutputStream stream=this.openFileOutput("music.cfg", Context.MODE_WORLD_WRITEABLE);
  7.             properties.store(stream, "");
  8.         } catch (FileNotFoundException e) {
  9.             // TODO: handle exception
  10.             return false;
  11.         }catch (IOException e) {
  12.             // TODO: handle exception
  13.             return false;
  14.         }
  15.         return true;
  16.         
  17.     }
复制代码

注意:

  • 1、 properties.store方法如果出现错误将会抛出IOException异常。
  • 2、properties 的KEY和value必须为 String 类型,如果不是String 类型将会抛出ClassCastException异常,请注意这点。

本篇全部代码参考如下:
Files 文件操作代码参考

  1. package com.terry;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.util.Properties;
  7. import android.app.Activity;
  8. import android.content.Context;
  9. import android.content.SharedPreferences;
  10. import android.os.Bundle;
  11. import android.view.KeyEvent;
  12. import android.widget.CheckBox;
  13. import android.widget.CompoundButton;
  14. import android.widget.TextView;
  15. import android.widget.CompoundButton.OnCheckedChangeListener;
  16. public class sharedPreActivity extends Activity {
  17.     private TextView myTextView;
  18.     private CheckBox myBox;
  19.     private playMusic PLAYER=null;
  20.     private boolean isplay=false;
  21.     /** Called when the activity is first created. */
  22.     @Override
  23.     public void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.main);
  26.         myTextView=(TextView)findViewById(R.id.TextView01);
  27.         
  28.         myBox=(CheckBox)findViewById(R.id.CheckBox01);
  29.         PLAYER=new playMusic(this);
  30.         /*
  31.          * 文件创建模式:Activity.MODE_APPEND
  32.          * 如果该文件已经存在,然后将数据写入,而不是抹掉它现有文件的末尾。
  33.          */
  34.         /*
  35.          * 文件创建模式:MODE_PRIVATE
  36.          * 默认模式,在那里创建的文件只能由应用程序调用,即为私有的
  37.          */
  38.         /*
  39.          * 文件创建模式:Activity.MODE_WORLD_READABLE
  40.          * 允许所有其他应用程序有读取和创建文件的权限。
  41.          */
  42.         /*
  43.          * 文件创建模式:Activity.MODE_WORLD_WRITEABLE
  44.          * 允许所有其他应用程序具有写入、访问和创建的文件权限。
  45.          */
  46.         
  47.         /*
  48.          * SharedPreferences---数据存储之获取
  49.         SharedPreferences settings=getPreferences(Activity.MODE_PRIVATE);
  50.         
  51.         isplay=settings.getBoolean("isplay", false); //通过key值找到value,如果不存在即返回false
  52.         
  53.         */
  54.         load();
  55.         myBox.setChecked(isplay);
  56.         if(isplay){
  57.             
  58.             myTextView.setText("当前音乐的播放状态:开");
  59.             isplay=true;
  60.             PLAYER.Play();
  61.         }
  62.         else{
  63.             myTextView.setText("当前音乐的播放状态:关");
  64.         }
  65.       
  66.         myBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
  67.             
  68.             @Override
  69.             public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
  70.                 // TODO Auto-generated method stub
  71.                 if(isChecked)
  72.                 {
  73.                     myTextView.setText("当前音乐的播放状态:开");
  74.                     isplay=true;
  75.                     PLAYER.Play();
  76.                 }
  77.                 else{
  78.                     myTextView.setText("当前音乐的播放状态:关");
  79.                     isplay=false;
  80.                     PLAYER.FreeMusc();
  81.                 }
  82.             }
  83.         });
  84.         
  85.     }
  86.    
  87.     @Override
  88.     public boolean onKeyDown(int keyCode, KeyEvent event) {
  89.         // TODO Auto-generated method stub
  90.         if(keyCode==KeyEvent.KEYCODE_BACK){
  91.             /*
  92.              * SharedPreferences  --数据存储之保存
  93.             SharedPreferences uiState=getPreferences(0);
  94.             SharedPreferences.Editor editor=uiState.edit();
  95.             editor.putBoolean("isplay", isplay);
  96.             editor.commit();
  97.             */
  98.             save();
  99.             if(isplay)
  100.             {
  101.                 PLAYER.FreeMusc();
  102.             }
  103.             this.finish();
  104.             return true;
  105.         }
  106.             
  107.         return super.onKeyDown(keyCode, event);
  108.     }
  109.    
  110.    
  111.     void load()
  112.     {
  113.         Properties properties=new Properties();
  114.         try {
  115.             FileInputStream stream =this.openFileInput("music.cfg");
  116.             properties.load(stream);
  117.         } catch (FileNotFoundException e) {
  118.             // TODO: handle exception
  119.             return;
  120.         } catch (IOException e) {
  121.             // TODO Auto-generated catch block
  122.             return;
  123.         }
  124.         isplay=Boolean.valueOf(properties.get("isplay").toString());
  125.     }
  126.    
  127.     boolean save()
  128.     {
  129.         Properties properties =new Properties();
  130.         properties.put("isplay", String.valueOf(isplay));
  131.         try {
  132.             FileOutputStream stream=this.openFileOutput("music.cfg", Context.MODE_WORLD_WRITEABLE);
  133.             properties.store(stream, "");
  134.         } catch (FileNotFoundException e) {
  135.             // TODO: handle exception
  136.             return false;
  137.         }catch (IOException e) {
  138.             // TODO: handle exception
  139.             return false;
  140.         }
  141.         return true;
  142.         
  143.     }
  144.    
  145. }
复制代码

6、查看 Files 产生的文件
上篇preferences 存储数据时,文件保存在shared_prefs文件夹下,如果我们在 Files产生文件的时候没有为其指定绝对路径,那么系统就会在 shared_prefs 相同的目录中产生一个名为files 的文件夹,里面就有我们写入的数据。如图:

运行效果如下:

Tip:如果你需要用一个文件来加载初始化程序 ,可以事先在目录下res/raw/tempFile中建立一个静态文件,这样就可以通过Resources.openRawResource(R.raw.文件名)来返回一个文件流,直读读取文件。

 

就到这里。

源码下载:

本帖隐藏的内容需要回复才可以浏览

 

 

本文位于博客地址为:http://www.cnblogs.com/TerryBlog/archive/2010/06/20/1761311.html

原创粉丝点击