在 Android 上,你可以通过以下方法之一保存文件:
1. 使用存储权限将文件保存到内部存储空间:
```java
String filename = "myfile.txt";
String fileContents = "Hello, world!";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(fileContents.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
```
2. 使用存储权限将文件保存到外部存储空间:
```java
String filename = "myfile.txt";
String fileContents = "Hello, world!";
File file = new File(Environment.getExternalStorageDirectory(), filename);
try {
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(fileContents.getBytes());
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
```
记得在 AndroidManifest.xml 文件中申请存储权限:
```xml
```
请注意,对于 Android 10 及更高版本,你需要使用 MediaStore API 或公共存储空间来保存文件。