android studio内部存储 实例
时间: 2024-04-26 17:18:23 浏览: 79
Android Studio内部存储是指在Android应用程序中,可以使用内部存储来保存应用程序的私有数据。以下是一个使用内部存储的示例:
1. 写入数据到内部存储:
```java
String filename = "myfile.txt";
String content = "Hello, world!";
try {
FileOutputStream fos = openFileOutput(filename, Context.MODE_PRIVATE);
fos.write(content.getBytes());
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
```
2. 从内部存储读取数据:
```java
String filename = "myfile.txt";
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);
}
br.close();
String content = sb.toString();
} catch (IOException e) {
e.printStackTrace();
}
```
3. 删除内部存储中的文件:
```java
String filename = "myfile.txt";
deleteFile(filename);
```
阅读全文