android what is wrong with openFileOutput?

Viewed 95016

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 ?

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

You can use openFileOutput in static Class if you pass View as below:

 public static void save(View v, String fileName , String message){

    FileOutputStream fos = null;

    try {

        fos = v.getContext().openFileOutput(fileName, Context.MODE_PRIVATE);
        fos.write(message.getBytes());

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

}
Related