在我的Android应用程序中,我在txt文件中记录错误。但是当我尝试创建文件时,我遇到了错误Java.io.IOException: open failed: EROS( Read Only file system error)
。我在AndroidManifest.xml
中添加了写入权限,但没有区别。怎么解决?
代码
try {
File file =new File("Log.txt");
if(!file.exists()){
file.createNewFile();
}
catch (Exception e)
{
Log.w("tun tun", e.toString());
}
答案 0 :(得分:1)
这样做
存储在外部存储设备(SDCARD)
try {
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "Log.txt");
if (!file.exists()) {
file.createNewFile();
}
} catch (Exception e) {
Log.w("tun tun", e.toString());
}
存储在内部存储空间
try {
File file = new File(context.getCacheDir() + File.separator + "Log.txt");
if (!file.exists()) {
file.createNewFile();
}
} catch (Exception e) {
Log.w("tun tun", e.toString());
}
答案 1 :(得分:1)
尝试以下代码: -
问题是你没有添加路径。
String filePath = context.getFilesDir().getPath().toString() + "/fileName.txt";
File f = new File(filePath);
或
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
或
path = Environment.getExternalStorageDirectory();
File file = new File(path, "/" + fname);