2012-05-28 19 views
11

Androidシステムで簡単なテキストファイルに書き込もうとしています。これは私のコードです:Android - 読み取り専用ファイルシステムIOException

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

誰もが、問題が何であるか任意のアイデアを持っている:私は次のように成功しません、私のマニフェストファイルへのアクセス権を追加しようとしていますか?

答えて

57

ファイルをrootに書き込もうとしているため、ファイルディレクトリにファイルパスを渡す必要があります。

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

これは私のために働きました。ありがとう。 –

2

は、開発者ガイドでは、このarticleからアプローチを使用してみてください:

String FILENAME = "hello_file"; 
String string = "hello world!"; 

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); 
fos.write(string.getBytes()); 
fos.close(); 
+0

これは私のために働くが、私は文字列を扱っている。 FileOutputStreamを使用してバイトの代わりに文字列を書き込むことはありますか? –

+1

彼が置くのは、文字列を書くことです。文字列のバイト表現だけです。ファイルをテキストエディタで表示したり、文字列として読み込んだりすると、書いたw/eの文字列表現が得られます。 – Jug6ernaut

関連する問題