Android文件存储方案详解:内部存储、外部存储、SQLite数据库
更新:2023-05-14 06:36 在Android开发中,文件读写是一项基本操作,为了支持文件的读写操作,Android系统提供了多种文件存储方式,如内部存储、外部存储和SQLite数据库等。本文将详细介绍这些文件存储方式的特点、使用场景及相关示例。
一、内部存储
内部存储是指将文件存储在应用程序的私有目录中,私有目录只能被本应用程序访问,其他应用程序无法访问。内部存储的优点是数据安全性高,不容易被其他应用程序篡改。以下是内部存储的使用方法:
1、创建文件
File file = new File(getFilesDir(), "example.txt");
try {
FileWriter writer = new FileWriter(file);
writer.write("Hello world!");
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
2、读取文件
File file = new File(getFilesDir(), "example.txt");
try {
FileReader reader = new FileReader(file);
char[] buf = new char[1024];
int len;
StringBuilder sb = new StringBuilder();
while ((len = reader.read(buf)) != -1) {
sb.append(new String(buf, 0, len));
}
reader.close();
String content = sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
3、删除文件
File file = new File(getFilesDir(), "example.txt");
if (file.exists()) {
file.delete();
}
二、外部存储
外部存储是指将文件存储在SD卡上,可以被多个应用程序共享,但是需要注意SD卡的插拔情况,如果没有进行判断,在SD卡未挂载的情况下使用会导致应用程序崩溃。以下是外部存储的使用方法:
1、创建文件
File file = new File(getExternalFilesDir(null), "example.txt");
try {
FileWriter writer = new FileWriter(file);
writer.write("Hello world!");
writer.flush();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
2、读取文件
File file = new File(getExternalFilesDir(null), "example.txt");
try {
FileReader reader = new FileReader(file);
char[] buf = new char[1024];
int len;
StringBuilder sb = new StringBuilder();
while ((len = reader.read(buf)) != -1) {
sb.append(new String(buf, 0, len));
}
reader.close();
String content = sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
3、删除文件
File file = new File(getExternalFilesDir(null), "example.txt");
if (file.exists()) {
file.delete();
}
三、SQLite数据库
SQLite数据库是一种轻量级的关系型数据库,适用于存储结构化数据,如应用程序的配置信息、用户数据等。以下是SQLite数据库的使用方法:
1、创建数据库
public class ExampleDatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "example.db";
private static final int DB_VERSION = 1;
public ExampleDatabaseHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS example(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
2、插入数据
ExampleDatabaseHelper databaseHelper = new ExampleDatabaseHelper(this);
SQLiteDatabase db = databaseHelper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("name", "Tom");
cv.put("age", 18);
db.insert("example", null, cv);
db.close();
3、查询数据
ExampleDatabaseHelper databaseHelper = new ExampleDatabaseHelper(this);
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Cursor cursor = db.query("example", null, null, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
} while (cursor.moveToNext());
cursor.close();
}
db.close();
4、删除数据
ExampleDatabaseHelper databaseHelper = new ExampleDatabaseHelper(this);
SQLiteDatabase db = databaseHelper.getWritableDatabase();
db.delete("example", "id=?", new String[]{"1"});
db.close();
通过以上示例代码,我们可以了解到Android中不同的文件存储方式的使用方法和适用情况。在实际开发中,我们可以根据需求选择合适的文件存储方式,实现数据的读写操作。