将HashMap写入文本文件Android studio

时间:2017-01-18 20:02:58

标签: java android hashmap

我曾尝试使用序列化将我的HashMap编写到文本文件中,但它似乎无法正常工作。我不确定我是否错了 我的Hashmap:     HashMap lettersAvailable = new HashMap(); 和我用于序列化的代码

public void onResume(){
        super.onResume();
        try
        {
            FileInputStream fileInputStream = new FileInputStream(getApplicationContext().getFilesDir()+"/FenceInformation.ser");
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
            Map lettersAvailable = (Map)objectInputStream.readObject();
            Toast.makeText(this,"Hashmap reloaded",Toast.LENGTH_SHORT);
        }
        catch(ClassNotFoundException | IOException | ClassCastException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onPause(){
        super.onPause();

        try
        {
            FileOutputStream fos = getApplicationContext().openFileOutput("YourInfomration.ser", Context.MODE_PRIVATE);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(lettersAvailable);
            oos.close();
            Log.d(TAG, "onCreate() Restoring previous state");
        } catch (IOException e) {
            e.printStackTrace();
        }


   }

在onResume方法中,它表示lettersAvailable变量从未在地图上使用lettersAvailable

有没有其他方法可以做到这一点?

0 个答案:

没有答案