Java openFileOutput引发异常

Java openFileOutput引发异常,java,android,file-io,android-emulator,Java,Android,File Io,Android Emulator,我正在android应用程序中将数据写入xml文件。我查阅了如何正确地执行文件IO,发现我需要使用openFileOutput。据我所知,我的代码没有任何问题,但它一直在抛出一个异常。我有一个try-catch包在下面的代码中: public void writeSearchXML(ArrayList<String> searches, String file) throws Exception { // try { // File newxmlfil

我正在android应用程序中将数据写入xml文件。我查阅了如何正确地执行文件IO,发现我需要使用openFileOutput。据我所知,我的代码没有任何问题,但它一直在抛出一个异常。我有一个try-catch包在下面的代码中:

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();
}
public void writeSearchXML(ArrayList搜索,字符串文件)
抛出异常{
//试一试{
//File newxmlfile=新文件(File);
//newxmlfile.createNewFile();
//}catch(异常e){
// }
试一试{
//下面的行每次都抛出一个异常
FileOutputStream out=openFileOutput(文件,
上下文。模式(世界可写);
}捕获(例外e){
抛出新异常(“文件输出流搜索失败”);
}
FileOutputStream fOut=openFileOutput(文件,
上下文。模式(世界可读);
//我们创建一个XmlSerializer来编写xml数据
XmlSerializer serializer=Xml.newSerializer();
//我们使用UTF-8将FileOutputStream设置为序列化程序的输出
//编码
serializer.setOutput(fOut,“UTF-8”);
//写我在这里找到了答案:


必须使用活动的上下文调用openFileOutput。

请提供错误日志。哪一行最麻烦?您可以做些什么来获得更多帮助:(1)将stacktrace添加到您的问题中-目前我们甚至不知道您会遇到什么样的异常;(2)从
openFileOutput
方法添加代码,因为该方法会引发异常。即将添加stacktrace…,
openFileOutput
方法内置于android中。它是
android.content.ContextWrapper.openFileOutput
的一部分。此外,
openFileOutput
引发的唯一异常是
fileNotFound
异常。在方法描述中,它说如果找不到文件,它将创建该文件。。。