android文件读写操作
时间: 2023-08-29 17:07:39 浏览: 59
在 Android 中,可以使用以下代码进行文件读写操作:
1. 文件读取操作:
```java
try {
FileInputStream fis = openFileInput("file.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
// 处理读取到的内容
}
br.close();
isr.close();
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
```
2. 文件写入操作:
```java
try {
FileOutputStream fos = openFileOutput("file.txt", Context.MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fos);
BufferedWriter bw = new BufferedWriter(osw);
bw.write("写入的内容");
bw.close();
osw.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
```
其中,`openFileInput()` 方法和 `openFileOutput()` 方法可以用于打开应用内部存储的文件。`Context.MODE_PRIVATE` 表示该文件只能被本应用访问,其他应用无法访问。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)