Android Studio(数据存储)

数据存储方式

方式 特点
文件存储 openFileInput()和openFileOutput()进行存写
SharedPreferences 以XML格式进行存储
SQLite 运算快、占用资源少、支持基本的sql语法
ContentProvider 可用于应用之间的数据交互
网络存储 通过网络提供的存储空间来存储/获取数据信息

文件存储

主要语法
java 复制代码
FileOutputStream fos = openFileOutput(String filename,int mode);
FileInputStream fis = openFileInput(String filename);
mode desc
Context.MODE_PRIVATE 该文件为当前程序私有
Context.MODE_APPEND 该文件的内容可以追加
Context.MODE_WORLD_READABLE 该文件的内容可以被其他程序"读"
Context.MODEL_WORLD_WRITEABLE 该文件的内容可以被其他程序"写"
代码示例
java 复制代码
//参考对象为内存:从内存输出即写入、输入到内存即读取

// 1.写入数据到文件
String fileName = "myfile.txt";
String data = "Hello, World!";
try {
    FileOutputStream fos = openFileOutput(fileName, Context.MODE_PRIVATE);
    fos.write(data.getBytes());
    fos.close();
} catch (IOException e) {
    e.printStackTrace();
}

// 2.从文件中读取数据
try {
    FileInputStream fis = openFileInput(fileName);
    InputStreamReader isr = new InputStreamReader(fis);
    BufferedReader br = new BufferedReader(isr);
    StringBuilder sb = new StringBuilder();
    String line;
    while ((line = br.readLine()) != null) {
        sb.append(line);
    }
    fis.close();
    String savedData = sb.toString();
} catch (IOException e) {
    e.printStackTrace();
}

SharedPreferences

简要

以XML方式的轻量级存储,适合存储少量的键值对数据,适用于简单的配置信息、用户偏好设置和应用程序状态等,比如登录的用户名。

语法
java 复制代码
// 存储数据到 SharedPreferences
SharedPreferences sharedPreferences = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("key", "value");
editor.apply();

// 从 SharedPreferences 中读取数据
String savedValue = sharedPreferences.getString("key", "default value");

SQLite

语法
java 复制代码
// 1. 创建实现类extends SQLiteOpenHelper
public class DBHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "myDb.db";
    private static final int DATABASE_VERSION = 1;
    private Context context;

    public DBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        this.context = context;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        
    }

}


// 2. 创建实现类对象,并调用相关方法实现CRUD
DBHelper dbHelper = new DBHelper(context);
SQLiteDatabase db = dbHelper.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM myTable", null);
while (cursor.moveToNext()){
    int id = cursor.getInt(cursor.getColumnIndexOrThrow("id"));
    ...
}
cursor.close();
ContentValues

SQLiteDatabase对象自身提供了一些CRUD方法,像表记录添加、修改,理应上需要我们传入多个字段(包括字段名和字段值),而为了解决这个问题就有了ContentValues对象,它允许我们给其增加多个键(字段名)、值(字段值);

所以当我们执行这个SQLiteDatabase对象的自身提供的添加或修改方法只用传入ContentValues即可。

可以参考下面代码例子:

java 复制代码
ContentValues values = new ContentValues();
values.put("name", "John Doe");
values.put("age", 30);
values.put("email", "johndoe@example.com");

// 插入数据到数据库
long newRowId = db.insert("myTable", null, values);

后言

至于ContentProvider和网络存储这两种存储方式只有结合具体的需求项目才能更好地学习,这里就不介绍了,需要学习的可以自己搜索相关文章具体学习。

相关推荐
阿巴斯甜14 小时前
Android 报错:Zip file '/Users/lyy/develop/repoAndroidLapp/l-app-android-ble/app/bu
android
Kapaseker15 小时前
实战 Compose 中的 IntrinsicSize
android·kotlin
xq952716 小时前
Andorid Google 登录接入文档
android
黄林晴17 小时前
告别 Modifier 地狱,Compose 样式系统要变天了
android·android jetpack
冬奇Lab1 天前
Android触摸事件分发、手势识别与输入优化实战
android·源码阅读
城东米粉儿1 天前
Android MediaPlayer 笔记
android
Jony_1 天前
Android 启动优化方案
android
阿巴斯甜1 天前
Android studio 报错:Cause: error=86, Bad CPU type in executable
android
张小潇1 天前
AOSP15 Input专题InputReader源码分析
android
_小马快跑_2 天前
Kotlin | 协程调度器选择:何时用CoroutineScope配置,何时用launch指定?
android