2010-12-01 25 views
1

私はアンドロイドアプリでxmlファイルにデータを書きます。ファイルI/Oを正しく行う方法を調べ、openFileOutputを使用する必要があることがわかりました。私の知る限りが私のコードには何の問題もありませんが、それは、私は次のコードでラップアラウンドのtry-catchを持っている行で例外を投げ続ける:すべてこのコードでは唯一の問題はあるopenFileOutput例外をスローする

public void writeSearchXML(ArrayList<String> searches, String file) 
     throws Exception { 
    // try { 
    // File newxmlfile = new File(file); 
    // newxmlfile.createNewFile(); 
    // } catch (Exception e) { 
    // } 
    try { 
     // THE FOLLOWING LINE THROWS AN EXCEPTION EVERY TIME 
     FileOutputStream out = openFileOutput(file, 
       Context.MODE_WORLD_WRITEABLE); 
    } catch (Exception e) { 
     throw new Exception("Failing on fileoutput stream searches."); 
    } 
    FileOutputStream fOut = openFileOutput(file, 
      Context.MODE_WORLD_READABLE); 
    // we create a XmlSerializer in order to write xml data 
    XmlSerializer serializer = Xml.newSerializer(); 
    // we set the FileOutputStream as output for the serializer, using UTF-8 
    // encoding 
    serializer.setOutput(fOut, "UTF-8"); 
    // Write <?xml declaration with encoding (if encoding not null) and 
    // standalone flag (if standalone not null) 
    serializer.startDocument(null, Boolean.valueOf(true)); 
    // set indentation option 
    serializer.setFeature(
      "http://xmlpull.org/v1/doc/features.html#indent-output", true); 
    // start a tag called "root" 
    serializer.startTag(null, "searches"); 
    for (String search : searches) { 
     serializer.startTag(null, "search"); 
     serializer.text(search); 
     serializer.endTag(null, "search"); 
    } 
    // // i indent code just to have a view similar to xml-tree 
    // serializer.startTag(null, "username"); 
    // serializer.text(username); 
    // serializer.endTag(null, "username"); 
    // serializer.startTag(null, "password"); 
    // serializer.text(password); 
    // serializer.endTag(null, "password"); 
    // //serializer.attribute(null, "attribute", "value"); 
    serializer.endTag(null, "searches"); 
    serializer.endDocument(); 
    // write xml data into the FileOutputStream 
    serializer.flush(); 
    // finally we close the file stream 
    fOut.close(); 
} 

ちょうどその1行。私はインターネット上のすべてを検索し、私は解決策を細かくすることができませんでした。これを修正するにはどうすればよいですか?

更新:スローされる例外はfileNotFound例外です。これは、私が、openFileOutput関数が、実際に記述されているようにファイルを実際に作成しないと信じさせています。 openFileOutputを使わずにファイルを作成するには、Androidで何ができますか?

+0

エラーログを提供してください。問題はどれですか? –

+0

あなたはもっと助けを得るために何ができるのですか?(1)あなたの質問にスタックトレースを追加する - 現時点では、どんな種類の例外が得られるか分からず、(2) 'openFileOutput'メソッドからコードを追加する例外がスローされます。 –

+0

Stacktrace ....を追加し、 'openFileOutput'メソッドがアンドロイドに組み込まれています。 'android.content.ContextWrapper.openFileOutput'の部分です – Graham

答えて