数据储存之Files

数据存储之Files
Properties
Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
引用
public class Propertiesextends Hashtable<Object,Object>


public void load(){
		//取得属性集
    	Properties properties = new Properties();
    	try {
    		//读取文件输入流
			FileInputStream stream = this.openFileInput("music.cfg");
			//从输入流中读取属性列表(键和元素对
			properties.load(stream);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			return;
		} catch (IOException e) {
			e.printStackTrace();
			return;
		}
		//取得数据
		mbMusic = Boolean.valueOf(properties.get("bmusic").toString());
    }


 public boolean save(){
    	Properties properties = new Properties();
    
    	properties.put("bmusic", String.valueOf(mbMusic));
    	try {
    		//
			[color=red]FileOutputStream stream = this.openFileOutput("music.cfg", Context.MODE_WORLD_WRITEABLE);[/color]
			//将打包好的数据写入文件
			properties.store(stream, "");
		} catch (FileNotFoundException e) {
			return false;
		} catch (IOException e) {
			return false;
		}
		return true;
    }



如果在开发一个应用程序时,需要通过加载一个文件的内容来初始化程序,就可以在编译程序之前,在res/raw/tempFile中建立一个static文件,这样可以在程序中通过Resources.openRawResources(R.raw.文件名)方法同样返回一个InputStream对象,直接读取文件内容。

============================================================================
Properties
一个 Properties 对象 是一个Hashtable 名跟值必须是字符串类型, 每个 属性 可以
有 一个 默认 的 Properties 列表 哪一个 指定的 默认的 可以 被 使用 当 一个 给予 名
找不到 在 这个 Properties 实力中

写入:
Properties properties = new Properties();
properties.put("", "");
FileOutputStream stream = this.openFileOutput("", Context.MODE_WORLD_WRITEABLE);
properties.store(stream, "");

读取:
Properties properties = new Properties();
FileInputStream stream = this.openFileInput("");
properties.load(stream);