Android: How to store data on internal memory?

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;

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.


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 .(ie 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

链接地址: http://www.djcxy.com/p/2590.html

上一篇: 使用Linux上的.NET / Mono来提供高容量的Web服务,一个好主意?

下一篇: Android:如何在内部存储器上存储数据?