【问题标题】:Android Beginner Save/Load small informationAndroid初学者保存/加载小资料
【发布时间】:2013-02-03 07:09:56
【问题描述】:

我希望在 android 中保存/加载一个非常简单的应用程序。 想法是检查 onCreate() 方法中是否存在文件,以及它是否在 onCreate 中加载设置。 这是执行此操作的正确功能吗? 保存在 onPause() 函数中完成。

我的做法是通过 FileOutputStream 和 FileInputStream。

但是我的代码看起来如何呢? 目前我有这个:

if (new File(FILENAME).isFile()) {
        FileInputStream fis = null;
        try {
            fis = openFileInput(FILENAME);
            fis.read();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        numOfEpisode = Integer.parseInt(fis.toString());
        numOfSeason = Integer.parseInt(fis.toString());
    }

忽略我还没有处理异常的事实,因为我知道测试时文件会在那里。

protected void onPause() {
    try {
        FileOutputStream fos = openFileOutput(FILENAME,
                Context.MODE_PRIVATE);
        fos.write(numOfEpisode);
        fos.write(numOfSeason);
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

当我关闭应用程序时(调用 onPause() 时),应用程序当前给我一个错误。 谁能指出我为什么?

另外我怎么知道 FileOutputStream.write() 以什么方式写我的语句?这是先进先出还是后进先出?

【问题讨论】:

  • 您有什么理由为此使用文件吗?如果您只保存 2 个 ints,我会将它们存储在 SharedPreferences

标签: android load save oncreate onpause


【解决方案1】:

我会为此使用 SharedPreferences,因为它更容易操作,并且专为从设备存储和检索少量信息而设计。查看How to use SharedPreferences in Android to store, fetch and edit values 了解这是如何完成的。

您将读取 onCreate 中的值并存储它们。当您 onPause 时,您会将它们写出到 SharedPreferences。

【讨论】:

  • SharedPreferences prefs = getPreferences(MODE_PRIVATE); numOfEpisode = prefs.getInt("numOfEpisode", 0); numOfSeason = prefs.getInt("numOfSeason", 0); 用于阅读 SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit(); editor.putInt("numOfEpisode", numOfEpisode); editor.putInt("numOfSeason", numOfSeason); editor.commit(); 用于写作。该功能有效(它确实可以读写),但是当我关闭我的应用程序时错误仍然存​​在。
  • 你需要调用 super.onPause() 例如protected void onPause() { super.onPause(); // insert everything else here // }
  • 当然!不敢相信我错过了。非常感谢。现在工作
【解决方案2】:

用这个代码试试吧:

File Gdir = new File(Environment.getExternalStorageDirectory()
                    .getPath() + "/NewFolder/");
            // have the object build the directory structure, if needed.

            if (!Gdir.exists()) {
                Gdir.mkdirs();
            }
                File outputFile = new File(Gdir, "file.txt");
                // now attach the OutputStream to the file object, instead of a
                // String representation
                try {                   
                    FileWriter mod = new FileWriter(
                            outputFile);
                    mod.write(numOfEpisode);
                    mod.write(numOfSeason);
                    mod.flush();
                    mod.close();

                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                Toast.makeText(getApplicationContext(), "File saved in: "+outputFile, Toast.LENGTH_LONG).show();

【讨论】:

    猜你喜欢
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-05
    • 2010-09-28
    相关资源
    最近更新 更多