Java android openFileOutput 有什么问题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3625837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 02:50:25  来源:igfitidea点击:

android what is wrong with openFileOutput?

javaandroid

提问by Fabien

I'm trying to use openFileOutput function but it doesn't compile and doesn't recognize the function. I'm using android sdk 1.6. Is this a sdk problem ? Is this a parameter problem ?

我正在尝试使用 openFileOutput 函数,但它无法编译并且无法识别该函数。我正在使用 android sdk 1.6。这是sdk问题吗?这是参数问题吗?

import java.io.FileOutputStream;
public static void save(String filename, MyObjectClassArray[] theObjectAr) {
    FileOutputStream fos;
    try {
        fos = openFileOutput(filename, Context.MODE_PRIVATE);

        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(theObjectAr); 
        oos.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch(IOException e){
        e.printStackTrace();
    }
}

采纳答案by naikus

Your method should be as follows. Takes in an extra Context as a parameter. To this method you can pass your Service or Activity

你的方法应该如下。接受一个额外的 Context 作为参数。您可以通过此方法传递您的服务或活动

public static void save(String filename, MyObjectClassArray[] theObjectAr, 
  Context ctx) {
        FileOutputStream fos;
        try {
            fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE);


            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(theObjectAr); 
            oos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
    }

回答by Konstantin Burov

You're trying invoke non-static method from static context (your method has static modifier). You either have to make your method to be non-static or to pass in an instance of Context (activity instance in most cases) and invoke the method on the object.

您正在尝试从静态上下文调用非静态方法(您的方法具有静态修饰符)。您要么必须使您的方法为非静态方法,要么传入一个 Context 实例(在大多数情况下为活动实例)并在对象上调用该方法。

回答by phyatt

Also you can't openOutputStreamon a path. It causes this exception:

你也不能openOutputStream在路上。它导致此异常:

java.lang.IllegalArgumentException: File /storage/sdcard0/path/to/file.txt contains a path separator

To fix this you need to create a file object and just create it like this:

要解决这个问题,您需要创建一个文件对象,然后像这样创建它:

String filename = "/sdcard/path/to/file.txt";
File sdCard = Environment.getExternalStorageDirectory();
filename = filename.replace("/sdcard", sdCard.getAbsolutePath());
File tempFile = new File(filename);
try
{
    FileOutputStream fOut = new FileOutputStream(tempFile);
    // fOut.write();
    // fOut.getChannel();
    // etc...
    fOut.close();
}catch (Exception e)
{
    Log.w(TAG, "FileOutputStream exception: - " + e.toString());
}