新聞中心
14.3.01

數(shù)據(jù)存儲(chǔ)(持久化)
五種:(外存)
1、Shared Preferences
Store private primitive data in key-value pairs.
參數(shù)共享(保存關(guān)鍵參數(shù):下載、登陸等)
2、Internal Storage
Store private data on the device memory.
內(nèi)部存儲(chǔ)
3、External Storage
Store public data on the shared external storage.
外部存儲(chǔ)(保存文件、安裝包)
4、SQLite Databases
Store structured data in a private database.
數(shù)據(jù)庫存儲(chǔ)(外存)
5、Network Connection
Store data on the web with your own network server.
網(wǎng)絡(luò)存儲(chǔ)
1、sharePreferences
應(yīng)用場(chǎng)景:應(yīng)用程序有少量的數(shù)據(jù)需要保存(設(shè)置參數(shù)的保存)
例子:按鈕點(diǎn)擊保存
//得到參數(shù)共享類型的對(duì)象
SharePreferences preference=getPreferences(0);//0:默認(rèn)值
//編輯器類型的對(duì)象
Editor edit=Preference.edit();
//想編輯器對(duì)象添加數(shù)據(jù)
Edit.putBoolean(“name”);
//提交
Commit();
獲取數(shù)據(jù)
getBoolean(“name”);//SS:默認(rèn)值
SharedPreferences preferences = getPreferences(0);
String name = preferences.getString("name", "Lily");
}
保存位置
Data/data/包名“xibo”,0);//xibo保存的文件名
2、InternalStorage
保存位置:
Data/data/包名/files/aa.txt"
保存
FileOutputStream openFileOutput =null;
openFileOutput = openFileOutput("aa.txt", MODE_PRIVATE);
openFileOutput.write(str.getBytes());
e.printStackTrace();
e.printStackTrace();
if(openFileOutput!=null){
openFileOutput.close();
e.printStackTrace();
}
}
讀取
FileInputStream
FileInputStream fis=null;
try {
fis=new FileInputStream("data/data/com.example.day_example/files/aa.txt");
byte[] buffer=newbyte[1024];
bab.append(buffer, 0, len);
String str=new String(bab.toByteArray(), 0, bab.length());
Log.e("getData------>", ""+bab.length());
e.printStackTrace();
e.printStackTrace();
if(fis!=null){
fis.close();
e.printStackTrace();
}
}
讀寫模式:
MODE_PRIVATE私有模式:只有當(dāng)前應(yīng)用才能對(duì)這個(gè)文件進(jìn)行讀寫操作,不會(huì)進(jìn) 行疊加,每次都清空
MODE_APPEND添加模式:只有當(dāng)前應(yīng)用才能對(duì)這個(gè)文件進(jìn)行讀寫操作,對(duì)數(shù)據(jù) 追加
MODE_WORLD_READABLE其他應(yīng)用可讀
MODE_WORLD_WRITEABLE其他應(yīng)用可寫
3、外部讀取
查找sd卡是否可用
Environment..MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
獲得
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
當(dāng)前標(biāo)題:數(shù)據(jù)存儲(chǔ)(持久化)-創(chuàng)新互聯(lián)
文章出自:http://m.biofuelwatch.net/article/igges.html


咨詢
建站咨詢
