Android - Read Only File System IOException(Android - 只读文件系统 IOException)
问题描述
我正在尝试在 Android 系统上写入一个简单的文本文件.这是我的代码:
I am trying to write to a simple text file on the Android system. This is my code:
public void writeClassName() throws IOException{
String FILENAME = "classNames";
EditText editText = (EditText) findViewById(R.id.className);
String className = editText.getText().toString();
File logFile = new File("classNames.txt");
if (!logFile.exists())
{
try
{
logFile.createNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append(className);
buf.newLine();
buf.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
但是,此代码会产生java.io.IOException:打开失败:EROFS(只读文件系统)"错误.我尝试按如下方式向我的清单文件添加权限,但没有成功:
However, this code produces a "java.io.IOException: open failed: EROFS (Read-only file system)" error. I have tried adding permissions to my Manifest file as follows with no success:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="hellolistview.com"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ClassView"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".AddNewClassView"
/>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
有谁知道问题出在哪里?
Anyone have any idea what the problem is?
推荐答案
因为你正在尝试将文件写入root,所以你需要将文件路径传递给你的文件目录.
Because you are trying to write the file to root, you need to pass the file path to your file directory.
例子
String filePath = context.getFilesDir().getPath().toString() + "/fileName.txt";
File f = new File(filePath);
这篇关于Android - 只读文件系统 IOException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android - 只读文件系统 IOException
基础教程推荐
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01