一、文件存储
使用IO流操作文件
二、SharedPreferences存储
- 使用键值对的方式来存储数据
- 数据存储在:工作目录/data/data/项目包名/shared_prefs/目录下
使用下方封装的工具类进行操作
public class DataHelper {
/**
* SharedPreferences方式存储
* @param context XML的文件名,通常取模块名
* @param key 存储在XML中的key
* @param value 值
*/
public static void putData2SharedPreferences(Context context, String moduleName, String key, Object value) {
SharedPreferences sharedPreferences = context.getSharedPreferences(moduleName, Context.MODE_PRIVATE); // 私有数据
Editor editor = sharedPreferences.edit();
if (value instanceof Boolean) {
editor.putBoolean(key, (Boolean) value);
} else if (value instanceof Integer) {
editor.putInt(key, (Integer) value);
} else if (value instanceof String) {
editor.putString(key, (String) value);
} else if (value instanceof Long) {
editor.putLong(key, (Long) value);
} else if (value instanceof Float) {
editor.putFloat(key, (Float) value);
}
editor.apply();
}
/**
* 在SP中读取String
* @param context
* @param name
* @param key
* @return
*/
public static String getStringFromSharedPreferences(Context context, String name , String key) {
SharedPreferences sharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE); // 私有数据
return sharedPreferences.getString(key, "");
}
/**
* 在SP中读取Boolean
* @param context
* @param name
* @param key
* @return
*/
public static Boolean getBooleanFromSharedPreferences(Context context, String name , String key) {
SharedPreferences sharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE); // 私有数据
return sharedPreferences.getBoolean(key, false);
}
/**
* 在SP中读取int
* @param context
* @param name
* @param key
* @return
*/
public static int getIntFromSharedPreferences(Context context, String name , String key) {
SharedPreferences sharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE); // 私有数据
return sharedPreferences.getInt(key, -1);
}
}
三、SQLite数据库存储
Android系统内置了数据库。SQLite是一款轻量级的关系型数据库
使用下方封装的工具类进行操作
public class UserDB {
private static String DB_Name = "UserInfo";
private static int DB_Version = 1;
private static SQLiteDatabase sqLiteDatabase;
public static class UserDBOpenHelper extends SQLiteOpenHelper{
private Context mContext;
/**
* 建表语句
* 字段:性别、年龄、昵称、账户、密码
*/
private static String SQL = "create table UserInfo (" +
" id integer primary key autoincrement," +
" account text," +
" name text," +
" sex text," +
" age integer ," +
" password text" +
")";
/**
* 构造函数:传入context
* @param context
*/
public UserDBOpenHelper(@Nullable Context context) {
super(context, DB_Name, null, DB_Version);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL); //执行建表语句
Toast.makeText(mContext,"Create Table succeeded",Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists UserInfo");//如果存在,则删除
db.execSQL(SQL);
}
}
/**
* 插入记录
* @param user
*/
public long InsertData(User user){
ContentValues contentValues = new ContentValues();
contentValues.put("account",user.getAccount());
contentValues.put("name",user.getName());
contentValues.put("sex",user.getSex());
contentValues.put("age",user.getAge());
contentValues.put("password",user.getPassword());
return sqLiteDatabase.insert("UserInfo",null,contentValues);
}
/**
* 打开数据库
* @param context
*/
public void OpenUserDB(Context context){
UserDBOpenHelper userDBOpenHelper = new UserDBOpenHelper(context);
sqLiteDatabase = userDBOpenHelper.getWritableDatabase();
}
/**
* 查询所有用户:将用户映射到一个用户集合
* @return
*/
public List<User> FindAllUsers(){
List<User> users = new ArrayList<>();
Cursor UserInfo = sqLiteDatabase.query("UserInfo", null,
null, null,
null, null, null);
while(UserInfo.moveToNext()){
String account = UserInfo.getString(UserInfo.getColumnIndex("account"));
String name = UserInfo.getString(UserInfo.getColumnIndex("name"));
String sex = UserInfo.getString(UserInfo.getColumnIndex("sex"));
int age = Integer.parseInt(UserInfo.getString(UserInfo.getColumnIndex("age")));
String password = UserInfo.getString(UserInfo.getColumnIndex("password"));
User user = new User(account,password,name,sex,age);
users.add(user);
}
return users;
}
/**
* 通过账户找到用用户记录映射到User对象
* select * from UserInfo where account = ?
*/
public User findUserByAccount(String srcAccount){
Cursor cursor = sqLiteDatabase.query("UserInfo",null,"account = ?",new String[]{srcAccount},
null,null,null);
User user = null;
while(cursor.moveToNext()) {
String account = cursor.getString(cursor.getColumnIndex("account"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String sex = cursor.getString(cursor.getColumnIndex("sex"));
int age = Integer.parseInt(cursor.getString(cursor.getColumnIndex("age")));
String password = cursor.getString(cursor.getColumnIndex("password"));
user = new User(account,password,name,sex,age);
}
return user;
}
/**
* 更新用户信息
* @param srcAccount
* @param account
* @param name
* @param sex
* @param age
* @param password
*/
public void UpdateByAccount(String srcAccount, String account,String name,String sex,String age,String password){
ContentValues values = new ContentValues();
values.put("account",account);
values.put("name",name);
values.put("password",password);
values.put("age",age);
values.put("sex",sex);
sqLiteDatabase.update("UserInfo",values,"account = ?",new String[]{srcAccount});
}
//根据账户名字删除数据库记录
public boolean DeleteUserName(String account) {
return sqLiteDatabase.delete("UserInfo","account = ?",new String[]{account} )> 0;
}
}