Write a read/write using Android internal storage
时间: 2024-01-04 16:03:02 浏览: 96
To read and write data using Android internal storage, you can follow these steps:
1. Get the application context:
```java
Context context = getApplicationContext();
```
2. Create a file object with the file name and path:
```java
File file = new File(context.getFilesDir(), "filename.txt");
```
3. To write data to the file, create a FileOutputStream object and write the data to it:
```java
String data = "Hello, World!";
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(data.getBytes());
outputStream.close();
```
4. To read data from the file, create a FileInputStream object and read the data from it:
```java
FileInputStream inputStream = new FileInputStream(file);
byte[] bytes = new byte[(int) file.length()];
inputStream.read(bytes);
inputStream.close();
String data = new String(bytes);
```
Note that you should always check for exceptions when reading and writing files. Also, it's important to remember that data stored in internal storage is only accessible to your application and cannot be accessed by other applications or users.
阅读全文