gpt4 book ai didi

java - Android - 只读文件系统 IOException

转载 作者:IT老高 更新时间:2023-10-28 23:26:24 24 4
gpt4 key购买 nike

我正在尝试在 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(只读文件系统)”错误。我尝试按如下方式向我的 list 文件添加权限,但没有成功:

<?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" />

有谁知道问题出在哪里?

最佳答案

因为你正在尝试将文件写入根目录,所以你需要将文件路径传递给你的文件目录。

例子

String filePath = context.getFilesDir().getPath().toString() + "/fileName.txt";
File f = new File(filePath);

关于java - Android - 只读文件系统 IOException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10787942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com