一.本地存儲(chǔ)
調(diào)用context的openFileOutput()方法,其中傳入兩個(gè)參數(shù)翻擒,filename和mode。mode現(xiàn)在只用2種:MODE_PRIVATE和MODE_APPEND店茶,分別代表覆蓋同名文件內(nèi)容和在后面續(xù)寫內(nèi)容。文件直接寫在/data/data/packagename/目錄下。代碼不難不說(shuō)了惦辛。
public void save(String s){
FileOutputStream out=null;
BufferedWriter writer=null;
try{
out=context.openFileOutput("data",Context.MODE_PRIVATE);
writer=new BufferedWriter(new OutputStreamWriter(out));
writer.write(string);
}catch (Exception e){
e.printStackTrace();
}finally{
try {
if(writer !=null){
writer.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
public String load(){
FileInputStream inputStream=null;
BufferedReader reader=null;
StringBuilder string =new StringBuilder();
try{
inputStream=context.openFileInput("data");
reader=new BufferedReader(new InputStreamReader(inputStream));
String line;
while((line=reader.readLine())!=null){
string.append(line);
}
}catch (Exception e){
e.printStackTrace();
}finally{
try {
if(reader !=null){
reader.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
return string.toString();
}
二.SharedPreferences
- Context中的getSharedPreferences()方法:目錄確定,只有一種MODE_PRIVATE方法表示只有當(dāng)前應(yīng)用程序可以操作仓手。
- Activity中的getPreferences()方法:基本同上胖齐,會(huì)把當(dāng)前的類名當(dāng)作文件名。
- PreferencesManager中的getDefaultSharedPreferences()方法:如下代碼
public void save(){
SharedPreferences.Editor editor=context.getSharedPreferences
("data",Context.MODE_PRIVATE).edit();
editor.putString("String","boring");
editor.putBoolean("boolean",true);
//int,float相同就不寫了
editor.apply();
}
public void load(){
SharedPreferences pref=context.getSharedPreferences("data",Context.MODE_PRIVATE);
String string =pref.getString("String","");//默認(rèn)值
//int,boolean,float不寫了
}
三.數(shù)據(jù)庫(kù)
1.簡(jiǎn)單介紹
SQLITE數(shù)據(jù)庫(kù):Android系統(tǒng)內(nèi)置的輕量級(jí)關(guān)系型數(shù)據(jù)庫(kù) 嗽冒。重要方法:這兩個(gè)方法都可以創(chuàng)建和打開(kāi)數(shù)據(jù)庫(kù)呀伙,并返回一個(gè)可以對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作的對(duì)象。區(qū)別在于添坊,如果磁盤空間已滿:
- getReadableDatabase()——返回的對(duì)象只以讀的方式打開(kāi)數(shù)據(jù)庫(kù)
- getWriteableDatabase()——出現(xiàn)異常
兩個(gè)方法的參數(shù):Context剿另,數(shù)據(jù)庫(kù)名,自定義Cursor(一般是null)贬蛙,當(dāng)前版本號(hào)(用于升級(jí))驰弄;路徑:/data/data/packagename/databases/目錄下。
2.建表:
本人對(duì)數(shù)據(jù)庫(kù)的知識(shí)掌握還停留在大一速客,并且當(dāng)時(shí)會(huì)的也不多戚篙,這里就用書上的栗子:
public class Database extends SQLiteOpenHelper {
public static final String CREAT_BOOK="create table Book ("
+"id integer primary key autoincrement,"
+"author text,"
+"price real,"
+"pages integer,"
+"name text)";
private Context context;
public Database(Context con, String name, SQLiteDatabase.CursorFactory factory,
int version){
super(con,name, factory,version);
context=con;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREAT_BOOK);
Toast.makeText(context,"success",Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
在Activity中調(diào)用
Database database;
database.getReadableDatabase();
3.更新數(shù)據(jù)庫(kù)&添加數(shù)據(jù)
首先更新數(shù)據(jù)庫(kù),要加入新的表溺职,這里叫CREAT_BAG;
然后在onUpgrade()方法中添加:
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists Book");
db.execSQL("drop table if exists BAG");
onCreate(db);
}
先把數(shù)據(jù)庫(kù)里的表刪了岔擂,然后在調(diào)用onCreate方法創(chuàng)建表。(感覺(jué)很不智能- -)浪耘。還需要在Activity中重寫構(gòu)造方法
database= new DataBase(this,"Textsql.db",null,2);
關(guān)于對(duì)數(shù)據(jù)的操作乱灵,CRUD(create,retrieve,update,delete),對(duì)getReadableDatabases()返回值SQLiteDatabase進(jìn)行操作七冲。
四.路徑問(wèn)題:
public static final String SDPATH = Environment .getExternalStorageDirectory().getAbsolutePath();
//獲取外部存儲(chǔ)的路徑返回絕對(duì)路徑的,其實(shí)就是你的SD卡的文件路徑
public String FilePath=SDPATH+"Packagename"+filename;
另:
- 1.Android 通過(guò)哪些方式實(shí)現(xiàn)應(yīng)用程序之見(jiàn)的數(shù)據(jù)共享:
File,Broadcast,service,sqlite,Content provider和intent.
忽然想到的面試題隨手寫一下痛倚。 - 2.原本不打算發(fā)布的但細(xì)想想很久沒(méi)發(fā)布過(guò)了,關(guān)于數(shù)據(jù)庫(kù)的部分會(huì)再詳細(xì)學(xué)習(xí)記錄澜躺。
- 3.本文從書上總結(jié)如有問(wèn)題請(qǐng)直接指出蝉稳,一起學(xué)習(xí)一起進(jìn)步