1. 文件存储:
private void save(String text) {
FileOutputStream out = null;
BufferedWriter writer = null;
try {
out = openFileOutput("data", Context.MODE_PRIVATE);
writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(text);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
private String load() {
BufferedReader reader = null;
FileInputStream in = null;
StringBuffer buffer = new StringBuffer();
try {
in = openFileInput("data");
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null){
buffer.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(reader != null){
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return buffer.toString();
}
2. sharePreference:
与文件储存相比,这种方式更为方便
一共有三种获取sharePreference的方法:
(1)context类中的 getSharePreference
(2)activity类 的getPreference ,会自动将当前活动的类名作为文件名
(3)PreferenceManager 中的静态类 getDefaultSharePreference:接受一个context,自动以当前应用程序的包名作为文件名
保存preference
btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences.Editor editor = getSharedPreferences("data", MODE_PRIVATE).edit();
editor.putString("name", "Tom");
editor.putInt("age", 19);
editor.putBoolean("married", false);
editor.apply();
}
});
重新加载preference
btn_restore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);
String name = pref.getString("name","");
int age = pref.getInt("age",0);
boolean married = pref.getBoolean("married",false);
Log.d(TAG,name);
Log.d(TAG,String.valueOf(age));
Log.d(TAG,String.valueOf(married));
}
});
3.SQList 数据库技术
使用adb命令查看数据库
adb shell
su root:获取root权限
cd data/data/package Name/databases
sqlite3 BookStore.db
.table
.schema
(1) 创建数据库
创建MyDatabaseHelper 继承 SQLiteOpenHelper
a.实现其方法
b.定义sql语句,在OnCreate中创建表:
db.execSQL(sql);
public class MyDatabaseHelper extends SQLiteOpenHelper {
public static String CREATE_BOOK = "Create table Book(" +
"id integer primary key autoincrement," +
"anther text," +
"price real," +
"pages integer," +
"name text )";
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);
Toast.makeText(mContext,"Database create succeeded",Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
c.在MainActivity中 使用MyOpenHelper,helper
(2)更新数据库
onUpdate(){ } 在创建helper时的版本号决定是否调用这个方法。注意,onCreate方法只能调用一次。
(3)CRUD 增删改查
4. 一个开源库 LitePal
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_create:
Connector.getDatabase();
break;
case R.id.btn_insert:
Book book = new Book();
book.setAuthor("zxr");
book.setPrice(25.5);
book.save();
break;
case R.id.btn_update:
Book book = new Book();
book.setPrice(99.9);
book.setAuthor("zxr");
book.updateAll("name = ? and auther = ?", "Story", "zxr");
//book.update(2);
break;
case R.id.btn_delete:
LitePal.delete(Book.class, 2);
LitePal.deleteAll(Book.class, "price > ?", "20");
break;
case R.id.btn_query:
Book first = LitePal.findFirst(Book.class);
Book last = LitePal.findLast(Book.class);
Book book1 = LitePal.find(Book.class, 1);
List<Book> bookList = LitePal.findAll(Book.class);
List<Book> bookList1 = LitePal.select("name", "auther").where("price>?", "10").order("piice").find(Book.class);
}
}