Android的 - 只读文件系统IOException异常 [英] Android - Read Only File System IOException

查看:239
本文介绍了Android的 - 只读文件系统IOException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写到Android系统上的一个简单的文本文件。这是我的code:

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();
       }

然而,这code产生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?

推荐答案

由于您试图将文件写入根,你需要的文件路径传递到你的文件目录。

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异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆