数据存储之Files
Properties
Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
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; }