Android:如何在内存上存储数据?

One*_*rld 7 file-io android fileoutputstream java-io

这里完美地描述如何做到这一点,唯一的问题是:他不知道这个功能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());
          }
}
Run Code Online (Sandbox Code Playgroud)

这些是我导入的相关包:

import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
Run Code Online (Sandbox Code Playgroud)

fre*_*ley 2

查看dev.android.com 上示例中的使用 FileOutputStrem 的示例。它应该让您了解如何正确使用它。

  • 您需要在上下文上调用 openFileOutput 。尝试`context.openFileOutput()` (4认同)