我正在尝试在 Android 系统上写入一个简单的文本文件。这是我的代码:
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(只读文件系统)”错误。我尝试按如下方式向我的清单文件添加权限,但没有成功:
<?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" />
有人知道问题是什么吗?