我可以创建文件。它会被创建在 /data/data/com.mypackage.app/files/myfile.txt 的位置。但是我希望能够将其创建在 Internal Storage/Android/data/com.mypackage.app/files/myfiles.txt 的位置。我该如何做呢?
代码:
public void createFile() {
File path = new File(this.getFilesDir().getPath());
String fileName = "myfile.txt";
String value = "example value";
File output = new File(path + File.separator + fileName);
try {
FileOutputStream fileout = new FileOutputStream(output.getAbsolutePath());
OutputStreamWriter outputWriter=new OutputStreamWriter(fileout);
outputWriter.write(value);
outputWriter.close();
//display file saved message
Toast.makeText(getBaseContext(), "File saved successfully!",
Toast.LENGTH_SHORT).show();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
更新:
我解决了这个问题。也许有人需要帮助。只需更改这一行即可。
File output = new File(getApplicationContext().getExternalFilesDir(null),"myfile.txt");