本文旨在复习总结Android持久化存储方法,只列出了主要代码以及少量批注,仅作为复习参考使用。
文件存储
所有文件默认放在/data/data/<packagename>/files/
目录下
文件写入
public void save(String inputText){
FileOutputStream out=null;
BufferedWriter writer=null;
try {
out=openFileOutput("data", Context.MODE_PRIVATE);
writer=new BufferedWriter(new OutputStreamWriter(out));
writer.write(inputText);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(writer!=null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
读取文件
public String load(){
FileInputStream in =null;
BufferedReader reader=null;
StringBuilder content=new StringBuilder();
try {
in=openFileInput("data");
reader=new BufferedReader(new InputStreamReader(in));
String line="";
while ((line=reader.readLine())!=null){
content.append(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return content.toString();
}
SharePreferences方法
SharePreference
文件默认放在/data/data/<packagename>/shared_prefs/目录下
SharedPreferences写入
SharedPreferences.Editor editor=getSharedPreferences("data",MODE_PRIVATE).edit();
editor.putString("et_inputText","sharePreferences test");
editor.commit();
SharedPreferences读取
SharedPreferences sp=getSharedPreferences("data",MODE_PRIVATE);
String input=sp.getString("et_inputText","请输入用户名");//第二个参数是为空的默认信息
SQLite数据库存储
SQLite
文件默认放在/data/data/<packagename>/databases/目录下
public class MyDataBaseHelper extends SQLiteOpenHelper{
private static final String CREATE_BOOK="create table Book("
+"id integer primary key autoincrement, "
+"author text, "
+"price real, "
+"pages integer, "
+"name text)";
private static final String CREATE_CATEGORY="create table Category("
+"id integer primary key autoincrement, "
+"category_name text, "
+"category_code integer)";
private Context mContext;
public MyDataBaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
mContext=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOOK);
db.execSQL(CREATE_CATEGORY);
Toast.makeText(mContext,"create succeed",Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists Book");
db.execSQL("drop table if exists Category");
onCreate(db);
}
创建数据库
dbHelper=new MyDataBaseHelper(this,"BookStore.db",null,2);
插入数据
SQLiteDatabase db=dbHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name","Effective Java");
values.put("author","Joshua Bloch");
values.put("pages",454);
values.put("price",16.96);
db.insert("Book",null,values);
更新数据
ContentValues values=new ContentValues();
values.put("price",198.00);
SQLiteDatabase db=dbHelper.getReadableDatabase();
db.update("Book",values,"name=?",new String[]{"Android Programme"});
删除行
SQLiteDatabase db = dbHelper.getWritableDatabase();
db.delete("Book", "pages > ?", new String[] { "500" });
ContentValues
知识android提供的一个便于数据库操作的类,为数据库操作提供便利,也可以直接使用数据库语句进行操作
参考书目《第一行代码》