【问题标题】:Android: save to internal fileAndroid:保存到内部文件
【发布时间】:2014-12-22 03:58:24
【问题描述】:

我正在使用此代码尝试将 int 保存到文件中然后检索它:

    ObjectOutputStream oos;
    try{
        oos=new ObjectOutputStream(new FileOutputStream(new File(getFilesDir()+"hs.dat")));
        oos.writeInt(hs);
    }
    catch (Exception e){}

    TextView txtRecord = (TextView) findViewById(R.id.txtRecord);
    try{
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream(getFilesDir()+"hs.dat"));
        hs=ois.readInt();
        txtRecord.setText(getString(R.string.recordStr) +" "+ hs);
    } catch (Exception e) {
        txtRecord.setText(getString(R.string.recordStr) +" "+ "0");
    }

即使我知道 int 不是 0,我总是得到“0”,这不是正确的做法吗?

哎哟~

【问题讨论】:

    标签: android file android-studio int save


    【解决方案1】:

    我认为这是因为该文件仍在使用中。
    您需要先关闭它以释放文件,以便ois 可以访问它。

    oos.writeInt(hs);
    oos.close(); //--> close the oos
    

    【讨论】:

      猜你喜欢
      • 2023-03-10
      • 2011-10-10
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-21
      相关资源
      最近更新 更多