32

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();
    }
}
Komal12
  • 3,227
  • 4
  • 14
  • 25
Fabien
  • 1,957
  • 8
  • 30
  • 42

4 Answers4

60

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

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();
        }
    }
naikus
  • 23,937
  • 4
  • 41
  • 43
  • 1
    I couldn't get this to work. I tried a passed context, `context.getApplicationContext()`, `Application.instance()` and `Application.instance().getApplicationContext()` for the ctx here and it always throws FileNotFoundException. – Matt Connolly May 23 '11 at 08:16
  • 2
    Turns out, I had a permissions problem. Definitely not obvious that a "Permission Denied" problem results in a `FileNotFoundException`. – Matt Connolly May 23 '11 at 21:24
  • where to check the file...which path..? – chinna_82 Feb 21 '12 at 06:43
  • 4
    `Context.MODE_PRIVATE` makes the file visible only to your app, just in case someone is wondering. – Mirko Nov 14 '12 at 09:40
  • @chinna_82 there is a method getpath() in https://developer.android.com/reference/java/io/File#getPath() – Ganesh Chowdhary Sadanala Oct 04 '18 at 16:48
6

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.

Konstantin Burov
  • 68,322
  • 16
  • 114
  • 93
3

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

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());
}
phyatt
  • 17,674
  • 5
  • 55
  • 73
0

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

}
Kerelos
  • 73
  • 4