Android 小项目之--数据存储【Files】(附源码)
生活随笔
收集整理的這篇文章主要介紹了
Android 小项目之--数据存储【Files】(附源码)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
繼上篇數(shù)據(jù)存儲,現(xiàn)在我們來講講另外一種數(shù)據(jù)存儲,Files。本篇講述步驟如下:
load方法代碼如下: 本篇Load方法代碼參考 ?void?load()?
????{
????????Properties?properties=new?Properties();
????????try?{
????????????FileInputStream?stream?=this.openFileInput("music.cfg");
????????????properties.load(stream);
????????}?catch?(FileNotFoundException?e)?{
????????????//?TODO:?handle?exception
????????????return;
????????}?catch?(IOException?e)?{
????????????//?TODO?Auto-generated?catch?block
????????????return;
????????}
????????isplay=Boolean.valueOf(properties.get("isplay").toString());
????} 注意:
????{
????????Properties?properties?=new?Properties();?
????????properties.put("isplay",?String.valueOf(isplay));
????????try?{
????????????FileOutputStream?stream=this.openFileOutput("music.cfg",?Context.MODE_WORLD_WRITEABLE);
????????????properties.store(stream,?"");
????????}?catch?(FileNotFoundException?e)?{
????????????//?TODO:?handle?exception
????????????return?false;
????????}catch?(IOException?e)?{
????????????//?TODO:?handle?exception
????????????return?false;
????????}
????????return?true;
????????
????} 注意:
?
import?java.io.FileInputStream;
import?java.io.FileNotFoundException;
import?java.io.FileOutputStream;
import?java.io.IOException;
import?java.util.Properties;
import?android.app.Activity;
import?android.content.Context;
import?android.content.SharedPreferences;
import?android.os.Bundle;
import?android.view.KeyEvent;
import?android.widget.CheckBox;
import?android.widget.CompoundButton;
import?android.widget.TextView;
import?android.widget.CompoundButton.OnCheckedChangeListener;
public?class?sharedPreActivity?extends?Activity?{
????private?TextView?myTextView;
????private?CheckBox?myBox;
????private?playMusic?PLAYER=null;
????private?boolean?isplay=false;
????/**?Called?when?the?activity?is?first?created.?*/
????@Override
????public?void?onCreate(Bundle?savedInstanceState)?{
????????super.onCreate(savedInstanceState);
????????setContentView(R.layout.main);
????????myTextView=(TextView)findViewById(R.id.TextView01);
????????
????????myBox=(CheckBox)findViewById(R.id.CheckBox01);
????????PLAYER=new?playMusic(this);?
????????/*
?????????*?文件創(chuàng)建模式:Activity.MODE_APPEND
?????????*?如果該文件已經(jīng)存在,然后將數(shù)據(jù)寫入,而不是抹掉它現(xiàn)有文件的末尾。
?????????*/?
????????/*
?????????*?文件創(chuàng)建模式:MODE_PRIVATE
?????????*?默認(rèn)模式,在那里創(chuàng)建的文件只能由應(yīng)用程序調(diào)用,即為私有的
?????????*/?
????????/*
?????????*?文件創(chuàng)建模式:Activity.MODE_WORLD_READABLE
?????????*?允許所有其他應(yīng)用程序有讀取和創(chuàng)建文件的權(quán)限。
?????????*/
????????/*
?????????*?文件創(chuàng)建模式:Activity.MODE_WORLD_WRITEABLE
?????????*?允許所有其他應(yīng)用程序具有寫入、訪問和創(chuàng)建的文件權(quán)限。
?????????*/
????????
????????/*
?????????*?SharedPreferences---數(shù)據(jù)存儲之獲取
????????SharedPreferences?settings=getPreferences(Activity.MODE_PRIVATE);
????????
????????isplay=settings.getBoolean("isplay",?false);?//通過key值找到value,如果不存在即返回false
????????
????????*/
????????load();
????????myBox.setChecked(isplay);
????????if(isplay){
????????????
????????????myTextView.setText("當(dāng)前音樂的播放狀態(tài):開");
????????????isplay=true;
????????????PLAYER.Play();
????????}
????????else{
????????????myTextView.setText("當(dāng)前音樂的播放狀態(tài):關(guān)");
????????}
???????
????????myBox.setOnCheckedChangeListener(new?OnCheckedChangeListener()?{
????????????
????????????@Override
????????????public?void?onCheckedChanged(CompoundButton?buttonView,?boolean?isChecked)?{
????????????????//?TODO?Auto-generated?method?stub
????????????????if(isChecked)
????????????????{
????????????????????myTextView.setText("當(dāng)前音樂的播放狀態(tài):開");
????????????????????isplay=true;
????????????????????PLAYER.Play();
????????????????}
????????????????else{
????????????????????myTextView.setText("當(dāng)前音樂的播放狀態(tài):關(guān)");
????????????????????isplay=false;
????????????????????PLAYER.FreeMusc();
????????????????}
????????????}
????????});
????????
????}
????
????@Override
????public?boolean?onKeyDown(int?keyCode,?KeyEvent?event)?{
????????//?TODO?Auto-generated?method?stub
????????if(keyCode==KeyEvent.KEYCODE_BACK){
????????????/*
?????????????*?SharedPreferences??--數(shù)據(jù)存儲之保存
????????????SharedPreferences?uiState=getPreferences(0);
????????????SharedPreferences.Editor?editor=uiState.edit();
????????????editor.putBoolean("isplay",?isplay);
????????????editor.commit();?
????????????*/
????????????save();
????????????if(isplay)
????????????{
????????????????PLAYER.FreeMusc();
????????????}
????????????this.finish();
????????????return?true;
????????}
????????????
????????return?super.onKeyDown(keyCode,?event);
????}
????
????
????void?load()?
????{
????????Properties?properties=new?Properties();
????????try?{
????????????FileInputStream?stream?=this.openFileInput("music.cfg");
????????????properties.load(stream);
????????}?catch?(FileNotFoundException?e)?{
????????????//?TODO:?handle?exception
????????????return;
????????}?catch?(IOException?e)?{
????????????//?TODO?Auto-generated?catch?block
????????????return;
????????}
????????isplay=Boolean.valueOf(properties.get("isplay").toString());
????}
????
????boolean?save()
????{
????????Properties?properties?=new?Properties();?
????????properties.put("isplay",?String.valueOf(isplay));
????????try?{
????????????FileOutputStream?stream=this.openFileOutput("music.cfg",?Context.MODE_WORLD_WRITEABLE);
????????????properties.store(stream,?"");
????????}?catch?(FileNotFoundException?e)?{
????????????//?TODO:?handle?exception
????????????return?false;
????????}catch?(IOException?e)?{
????????????//?TODO:?handle?exception
????????????return?false;
????????}
????????return?true;
????????
????}
????
} 6、查看 Files 產(chǎn)生的文件 上篇preferences 存儲數(shù)據(jù)時,文件保存在shared_prefs文件夾下,如果我們在 Files產(chǎn)生文件的時候沒有為其指定絕對路徑,那么系統(tǒng)就會在 shared_prefs 相同的目錄中產(chǎn)生一個名為files 的文件夾,里面就有我們寫入的數(shù)據(jù)。如圖: 運行效果如下: Tip:如果你需要用一個文件來加載初始化程序 ,可以事先在目錄下res/raw/tempFile中建立一個靜態(tài)文件,這樣就可以通過Resources.openRawResource(R.raw.文件名)來返回一個文件流,直讀讀取文件。 就到這里。 源碼下載:/Files/TerryBlog/FilesDemo.rar
本文轉(zhuǎn)自 terry_龍 51CTO博客,原文鏈接:http://blog.51cto.com/terryblog/336755,如需轉(zhuǎn)載請自行聯(lián)系原作者
- 1、溫故而知新,復(fù)習(xí)四種數(shù)據(jù)存儲的區(qū)別。
- 2、什么是 Files 數(shù)據(jù)存儲。
- 3、什么是 Properties ?
- 4、Properties 重要方法和屬性講解。
- 5、模擬用戶設(shè)置參數(shù)。
- 6、查看 Files 產(chǎn)生的文件。
- Shared Preferences?
用來存儲“鍵-值”對的格式數(shù)據(jù)。是一個輕量級的鍵值對存儲機制,只可以存儲基本數(shù)據(jù)類型。
你可以參考上篇文章了解和使用它的用法:Android 小項目之--數(shù)據(jù)存儲【Shared Preferences】(附源碼)? - Files
它通過 FileInputStream 和 FileOutputStream 對文件進行操作。本篇將講述如何使用Files 數(shù)據(jù)存儲。 - SQLite
Android 提供的一個標(biāo)準(zhǔn)數(shù)據(jù)庫,支持SQL語句。
你可以參考這篇文章大致了解和使用它的用法:Android 小項目之--SQLite 使用法門 (附源碼)? - NetWork
通過網(wǎng)絡(luò)來存儲和獲得數(shù)據(jù)。
- File 就是把需要保存的東西通過文件的形式訊錄下來,當(dāng)需要這些數(shù)據(jù)時,通過讀取這個文件來獲取這些數(shù)據(jù)。因為 Android 采用了 Linux 核心,所以在Android 系統(tǒng)中,文件也是Linux 的形式。
- Android 中可以在設(shè)備本身的的存儲或者外接的存儲設(shè)備中創(chuàng)建用于保存數(shù)據(jù)的文件。同時,在默認(rèn)狀態(tài)下,文件是不能在不同的程序間共享的。
- 返回值:String?
方法:getProperty(String?name,?String?defaultValue)
解釋:通過指定的 “name“ 即Key,搜索屬性,參數(shù)二為默認(rèn)值,即通過Key找不到文件中的屬性時,要返回的默認(rèn)值。 - 返回值:String
方法:getProperty(String?name)?
解釋:通過指定的 ”name“ 即為 Key,搜索屬性,沒有返回默認(rèn)值。 - 無返回值: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)
解釋:注意,這種保存方法己經(jīng)過時,Google 不推薦使用此種寫法,這種方法忽略任何IO 異常,所以在實際操作過程中,可能會發(fā)生不必要的異常。 - 返回值:object
方法:setProperty(String?name,?String?value)
解釋:設(shè)置屬性,保存一個”鍵-值“對的屬性。 - 無返回值:synchronized void
方法:store(OutputStream?out,?String?comment)
解釋:通過 FileOutputStream 打開對應(yīng)的程序文件,然后通過Store 保存之前 Properties 打包好的數(shù)據(jù)。這里備注可以為空。 - 無返回值:void
方法:storeToXML(OutputStream?os,?String?comment)
解釋:通過FileOutputStream 打開對應(yīng)的程序文件,將打包好的數(shù)據(jù)寫入到XML文件。 - 無返回值:synchronized void
方法:storeToXML(OutputStream?os,?String?comment,?String?encoding)
解釋:通過FileOutputStream 打開對應(yīng)的程序文件,將打包好的數(shù)據(jù)寫入到XML文件,第三個參數(shù)可以指定編碼。
load方法代碼如下: 本篇Load方法代碼參考 ?void?load()?
????{
????????Properties?properties=new?Properties();
????????try?{
????????????FileInputStream?stream?=this.openFileInput("music.cfg");
????????????properties.load(stream);
????????}?catch?(FileNotFoundException?e)?{
????????????//?TODO:?handle?exception
????????????return;
????????}?catch?(IOException?e)?{
????????????//?TODO?Auto-generated?catch?block
????????????return;
????????}
????????isplay=Boolean.valueOf(properties.get("isplay").toString());
????} 注意:
- 1、?properties.load方法如果遇到錯誤將拋出?IOException?異常,并且使用的編碼為:ISO8859 - 1
- 2、加載的數(shù)據(jù)出現(xiàn)空格將會被忽略。
- 3、不要在你的Files 文件中加注釋 ,因為load的時候?qū)雎阅愕淖⑨尫枴?/li>
- 4、公認(rèn)的轉(zhuǎn)義序列:“\”,“\ \”,“\ r”開始,“\ ?”,“\!”,“\#”,“\ t”的,“\ b”與“\ f”的,和“\ uXXXX”(Unicode字符)。
????{
????????Properties?properties?=new?Properties();?
????????properties.put("isplay",?String.valueOf(isplay));
????????try?{
????????????FileOutputStream?stream=this.openFileOutput("music.cfg",?Context.MODE_WORLD_WRITEABLE);
????????????properties.store(stream,?"");
????????}?catch?(FileNotFoundException?e)?{
????????????//?TODO:?handle?exception
????????????return?false;
????????}catch?(IOException?e)?{
????????????//?TODO:?handle?exception
????????????return?false;
????????}
????????return?true;
????????
????} 注意:
- 1、?properties.store方法如果出現(xiàn)錯誤將會拋出IOException異常。
- 2、properties 的KEY和value必須為 String 類型,如果不是String 類型將會拋出ClassCastException異常,請注意這點。
?
import?java.io.FileInputStream;
import?java.io.FileNotFoundException;
import?java.io.FileOutputStream;
import?java.io.IOException;
import?java.util.Properties;
import?android.app.Activity;
import?android.content.Context;
import?android.content.SharedPreferences;
import?android.os.Bundle;
import?android.view.KeyEvent;
import?android.widget.CheckBox;
import?android.widget.CompoundButton;
import?android.widget.TextView;
import?android.widget.CompoundButton.OnCheckedChangeListener;
public?class?sharedPreActivity?extends?Activity?{
????private?TextView?myTextView;
????private?CheckBox?myBox;
????private?playMusic?PLAYER=null;
????private?boolean?isplay=false;
????/**?Called?when?the?activity?is?first?created.?*/
????@Override
????public?void?onCreate(Bundle?savedInstanceState)?{
????????super.onCreate(savedInstanceState);
????????setContentView(R.layout.main);
????????myTextView=(TextView)findViewById(R.id.TextView01);
????????
????????myBox=(CheckBox)findViewById(R.id.CheckBox01);
????????PLAYER=new?playMusic(this);?
????????/*
?????????*?文件創(chuàng)建模式:Activity.MODE_APPEND
?????????*?如果該文件已經(jīng)存在,然后將數(shù)據(jù)寫入,而不是抹掉它現(xiàn)有文件的末尾。
?????????*/?
????????/*
?????????*?文件創(chuàng)建模式:MODE_PRIVATE
?????????*?默認(rèn)模式,在那里創(chuàng)建的文件只能由應(yīng)用程序調(diào)用,即為私有的
?????????*/?
????????/*
?????????*?文件創(chuàng)建模式:Activity.MODE_WORLD_READABLE
?????????*?允許所有其他應(yīng)用程序有讀取和創(chuàng)建文件的權(quán)限。
?????????*/
????????/*
?????????*?文件創(chuàng)建模式:Activity.MODE_WORLD_WRITEABLE
?????????*?允許所有其他應(yīng)用程序具有寫入、訪問和創(chuàng)建的文件權(quán)限。
?????????*/
????????
????????/*
?????????*?SharedPreferences---數(shù)據(jù)存儲之獲取
????????SharedPreferences?settings=getPreferences(Activity.MODE_PRIVATE);
????????
????????isplay=settings.getBoolean("isplay",?false);?//通過key值找到value,如果不存在即返回false
????????
????????*/
????????load();
????????myBox.setChecked(isplay);
????????if(isplay){
????????????
????????????myTextView.setText("當(dāng)前音樂的播放狀態(tài):開");
????????????isplay=true;
????????????PLAYER.Play();
????????}
????????else{
????????????myTextView.setText("當(dāng)前音樂的播放狀態(tài):關(guān)");
????????}
???????
????????myBox.setOnCheckedChangeListener(new?OnCheckedChangeListener()?{
????????????
????????????@Override
????????????public?void?onCheckedChanged(CompoundButton?buttonView,?boolean?isChecked)?{
????????????????//?TODO?Auto-generated?method?stub
????????????????if(isChecked)
????????????????{
????????????????????myTextView.setText("當(dāng)前音樂的播放狀態(tài):開");
????????????????????isplay=true;
????????????????????PLAYER.Play();
????????????????}
????????????????else{
????????????????????myTextView.setText("當(dāng)前音樂的播放狀態(tài):關(guān)");
????????????????????isplay=false;
????????????????????PLAYER.FreeMusc();
????????????????}
????????????}
????????});
????????
????}
????
????@Override
????public?boolean?onKeyDown(int?keyCode,?KeyEvent?event)?{
????????//?TODO?Auto-generated?method?stub
????????if(keyCode==KeyEvent.KEYCODE_BACK){
????????????/*
?????????????*?SharedPreferences??--數(shù)據(jù)存儲之保存
????????????SharedPreferences?uiState=getPreferences(0);
????????????SharedPreferences.Editor?editor=uiState.edit();
????????????editor.putBoolean("isplay",?isplay);
????????????editor.commit();?
????????????*/
????????????save();
????????????if(isplay)
????????????{
????????????????PLAYER.FreeMusc();
????????????}
????????????this.finish();
????????????return?true;
????????}
????????????
????????return?super.onKeyDown(keyCode,?event);
????}
????
????
????void?load()?
????{
????????Properties?properties=new?Properties();
????????try?{
????????????FileInputStream?stream?=this.openFileInput("music.cfg");
????????????properties.load(stream);
????????}?catch?(FileNotFoundException?e)?{
????????????//?TODO:?handle?exception
????????????return;
????????}?catch?(IOException?e)?{
????????????//?TODO?Auto-generated?catch?block
????????????return;
????????}
????????isplay=Boolean.valueOf(properties.get("isplay").toString());
????}
????
????boolean?save()
????{
????????Properties?properties?=new?Properties();?
????????properties.put("isplay",?String.valueOf(isplay));
????????try?{
????????????FileOutputStream?stream=this.openFileOutput("music.cfg",?Context.MODE_WORLD_WRITEABLE);
????????????properties.store(stream,?"");
????????}?catch?(FileNotFoundException?e)?{
????????????//?TODO:?handle?exception
????????????return?false;
????????}catch?(IOException?e)?{
????????????//?TODO:?handle?exception
????????????return?false;
????????}
????????return?true;
????????
????}
????
} 6、查看 Files 產(chǎn)生的文件 上篇preferences 存儲數(shù)據(jù)時,文件保存在shared_prefs文件夾下,如果我們在 Files產(chǎn)生文件的時候沒有為其指定絕對路徑,那么系統(tǒng)就會在 shared_prefs 相同的目錄中產(chǎn)生一個名為files 的文件夾,里面就有我們寫入的數(shù)據(jù)。如圖: 運行效果如下: Tip:如果你需要用一個文件來加載初始化程序 ,可以事先在目錄下res/raw/tempFile中建立一個靜態(tài)文件,這樣就可以通過Resources.openRawResource(R.raw.文件名)來返回一個文件流,直讀讀取文件。 就到這里。 源碼下載:/Files/TerryBlog/FilesDemo.rar
本文轉(zhuǎn)自 terry_龍 51CTO博客,原文鏈接:http://blog.51cto.com/terryblog/336755,如需轉(zhuǎn)載請自行聯(lián)系原作者
總結(jié)
以上是生活随笔為你收集整理的Android 小项目之--数据存储【Files】(附源码)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 视频营销:影响视频排名的五个重要因素
- 下一篇: Docker应用的四个关键设计因素