利用Android studio 文件存储
时间: 2023-08-31 21:30:12 浏览: 100
在Android Studio中存储文件有多种方式,包括内部存储、外部存储、SQLite数据库等。下面是一些常用的方法:
1. 内部存储
使用内部存储可以将文件保存在应用程序私有目录中,其他应用程序无法访问该目录。以下是一些示例代码:
- 写入文件:
```java
String filename = "myfile";
String fileContents = "Hello world!";
try (FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE)) {
fos.write(fileContents.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
```
- 读取文件:
```java
String filename = "myfile";
try (FileInputStream fis = openFileInput(filename)) {
InputStreamReader inputStreamReader = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append('\n');
}
String fileContents = stringBuilder.toString();
} catch (IOException e) {
e.printStackTrace();
}
```
2. 外部存储
使用外部存储可以将文件保存在SD卡中,其他应用程序也可以访问该目录。以下是一些示例代码:
- 检查是否有权限:
```java
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
```
- 写入文件:
```java
File file = new File(Environment.getExternalStorageDirectory(), "myfile");
String fileContents = "Hello world!";
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write(fileContents.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
```
- 读取文件:
```java
File file = new File(Environment.getExternalStorageDirectory(), "myfile");
try (FileInputStream fis = new FileInputStream(file)) {
InputStreamReader inputStreamReader = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append('\n');
}
String fileContents = stringBuilder.toString();
} catch (IOException e) {
e.printStackTrace();
}
```
3. SQLite数据库
SQLite是一种轻量级的关系型数据库,常用于存储结构化的数据。以下是一些示例代码:
- 创建数据库:
```java
public class MyDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mydatabase.db";
private static final int DATABASE_VERSION = 1;
public MyDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS mytable (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS mytable");
onCreate(db);
}
}
```
- 插入数据:
```java
MyDatabaseHelper dbHelper = new MyDatabaseHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "John");
values.put("age", 25);
db.insert("mytable", null, values);
db.close();
```
- 查询数据:
```java
MyDatabaseHelper dbHelper = new MyDatabaseHelper(this);
SQLiteDatabase db = dbHelper.getReadableDatabase();
String[] projection = {"_id", "name", "age"};
Cursor cursor = db.query("mytable", projection, null, null, null, null, null);
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
}
cursor.close();
db.close();
```
希望这些示例代码能够帮助你在Android Studio中存储文件。
阅读全文