摘要:本文旨在复习总结持久化存储方法,只列出了主要代码以及少量批注,仅作为复习参考使用。
本文旨在复习总结Android持久化存储方法,只列出了主要代码以及少量批注,仅作为复习参考使用。
文件存储所有文件默认放在/data/data/
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/
SharedPreferences.Editor editor=getSharedPreferences("data",MODE_PRIVATE).edit();
editor.putString("et_inputText","sharePreferences test");
editor.commit();
SharedPreferences sp=getSharedPreferences("data",MODE_PRIVATE);
String input=sp.getString("et_inputText","请输入用户名");//第二个参数是为空的默认信息
SQLite数据库存储
SQLite文件默认放在/data/data/
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提供的一个便于数据库操作的类,为数据库操作提供便利,也可以直接使用数据库语句进行操作
参考书目《第一行代码》
我的文章列表
Email:sxh13208803520@gmail.com
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/66961.html
摘要:此时再次旋转屏幕时,该不会被系统杀死和重建,只会调用。因此可通过和来判断是否被重建,并取出数据进行恢复。但需要注意的是,在取出数据时一定要先判断是否为空。只有在进程不被掉,正常情况下才会执行方法。 目录介绍 1.0.0.1 说下Activity的生命周期?屏幕旋转时生命周期?异常条件会调用什么方法? 1.0.0.2 后台的Activity被系统回收怎么办?说一下onSaveInsta...
阅读 2656·2023-04-25 22:42
阅读 2483·2021-09-22 15:16
阅读 3635·2021-08-30 09:44
阅读 627·2019-08-29 16:44
阅读 3450·2019-08-29 16:20
阅读 2686·2019-08-29 16:12
阅读 3534·2019-08-29 16:07
阅读 809·2019-08-29 15:08