Android持久化存储总结
16lz
2021-01-25
本文旨在复习总结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写入
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/
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提供的一个便于数据库操作的类,为数据库操作提供便利,也可以直接使用数据库语句进行操作
参考书目《第一行代码》
更多关于java的文章请戳这里:(您的留言意见是对我最大的支持)
我的文章列表
Email:sxh13208803520@gmail.com
更多相关文章
- 一款常用的 Squid 日志分析工具
- GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
- RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
- Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
- Adroid studio3.2 JNI教程
- Android(安卓)WebView使用本地字体
- Android开发小结1
- Mac系统下Android(安卓)SDK无法更新解决办法
- Android(安卓)学习笔记 databinding简单使用:使用databinding在li