Question

It's perfectly described here how to do it, the only problem: He doesnt know the function openFileOutput();

private void saveSettingsFile() {
          String FILENAME = "settings";
          String string = "hello world!";

          FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); //openFileOutput underlined red
          try {
            fos.write(string.getBytes());
            fos.close();
          } catch (IOException e) {
            Log.e("Controller", e.getMessage() + e.getLocalizedMessage() + e.getCause());
          }
}

Those are the relevant packages I imported:

import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
Was it helpful?

Solution

Have a look at this example of using a FileOutputStrem from the Examples on dev.android.com. It should give you an idea of how to use it correctly.

OTHER TIPS

Class within which this method is declared, is defined as "Static". thats why it is throwing error. Remove static from the class definition and bingo...

Just add a "try catch" block and put them in between this.

Like this:

    private void saveSettingsFile(String FILENAME, String data) {

    FileOutputStream fos;
    try {
        fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
        fos.write(data.getBytes());
        fos.close();
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } // openFileOutput underlined red
    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

When there is red line under the line.. First check that the line is under the full sentense or only right side of the sentense .(i.e after equal sign).

If it covers the whole line, then it has to fix some bugs..

Or if it under only the right side of the sentense ...Then it must wants some exception handling things.

If you don't know what type of exception it may generate...
Dont fear , just write all the code in a try block( try{ } ) and then add a catch and pass a Exception object inside catch .. Now its fine..

Like this :

  try
  {
    ...........your code
    ......
  } 
   catch(Exception e)
  {
   e.printstacktrace();

  }

Now all are fine.

Thank you

openFileOutput is a method of Context object. And don't forget to add finally clause to close the stream. Bellow is an example (a bit clumsy because of Java 6 because of Android).

String data = "Hello";
FileOutputStream fos = null;
try {
    fos = mContext.openFileOutput(FILENAME, Context.MODE_PRIVATE);
    fos.write(data.getBytes(Charset.defaultCharset()));
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (fos != null) {
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

mContext variable should be defined somewhere above and initialized like mContext = getApplicationContext() if you are inside of an activity

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top