写/读任何对象/于Android内存 [英] Write/Read any object to/from Android memory

查看:127
本文介绍了写/读任何对象/于Android内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在写的方法:


  • 从内存中读取对象

  • 写对象的内存

我试图挽救一个String对象,并看了一遍,但我的code没有工作。

这是我的code:

 公共无效writeObjectToMemory(字符串文件名,Object对象){
        FileOutputStream中FOS;
        尝试{
            FOS = game.openFileOutput(文件名,Context.MODE_PRIVATE);
            ObjectOutputStream的OS =新的ObjectOutputStream(FOS);
            os.writeObject(对象);
            os.close();
        }
        赶上(FileNotFoundException异常五){
            e.printStackTrace();        }
        赶上(IOException异常五){
            e.printStackTrace();        }    }    公共无效readObjectFromMemory(字符串文件名,Object对象){
        FIS的FileInputStream;
        尝试{
            FIS = game.openFileInput(文件名);
            ObjectInputStream的是=新的ObjectInputStream(FIS);
            对象= is.​​readObject();
            is.close();
        }
        赶上(FileNotFoundException异常五){
            e.printStackTrace();        }
        赶上(StreamCorruptedException E){
            e.printStackTrace();        }
        赶上(IOException异常五){
            e.printStackTrace();        }
        捕捉(ClassNotFoundException的E){
            e.printStackTrace();        }    }


解决方案

您写道:

 公共无效readObjectFromMemory(字符串文件名,Object对象){
    // ...
    对象= is.​​readObject();

对象只有你传递给函数的参数的副本。您可以更改复制方法中(像你一样),但这会对实际参数没有影响。

您必须返回你的对象,而不是:

 公共对象readObjectFromMemory(字符串文件名){
    FIS的FileInputStream;
    obj对象;
    尝试{
        FIS = game.openFileInput(文件名);
        ObjectInputStream的是=新的ObjectInputStream(FIS);
        物镜= is.​​readObject();
        is.close();
    }
    赶上(...){
        返回null;
    }    返回OBJ;
}

阅读此了解更多详情: HTTP://www.cs.utoronto。 CA /〜dianeh /教程/ PARAMS /

I'm currently writing to methods to:

  • read object from memory
  • write object to memory

I tried to save a String object and read it again, yet my code didn't work.

This is my code:

public void writeObjectToMemory(String filename, Object object) {
        FileOutputStream fos;
        try {
            fos = game.openFileOutput(filename, Context.MODE_PRIVATE);
            ObjectOutputStream os = new ObjectOutputStream(fos);
            os.writeObject(object);
            os.close();
        } 
        catch (FileNotFoundException e) {
            e.printStackTrace();

        } 
        catch (IOException e) {
            e.printStackTrace();

        }

    }

    public void readObjectFromMemory(String filename, Object object) {
        FileInputStream fis;
        try {
            fis = game.openFileInput(filename);
            ObjectInputStream is = new ObjectInputStream(fis);
            object = is.readObject();
            is.close();
        } 
        catch (FileNotFoundException e) {
            e.printStackTrace();

        } 
        catch (StreamCorruptedException e) {
            e.printStackTrace();

        } 
        catch (IOException e) {
            e.printStackTrace();

        } 
        catch (ClassNotFoundException e) {
            e.printStackTrace();

        }

    }

解决方案

You wrote :

public void readObjectFromMemory(String filename, Object object) {
    //...
    object = is.readObject();

But object is only a copy of the argument you passed to the function. You can change that copy inside the method (as you do), but this will have no effect on the actual parameter.

You have to return your object instead :

public Object readObjectFromMemory(String filename) {
    FileInputStream fis;
    Object obj;
    try {
        fis = game.openFileInput(filename);
        ObjectInputStream is = new ObjectInputStream(fis);
        obj = is.readObject();
        is.close();
    } 
    catch (...) {
        return null;
    }

    return obj;
}

Read this for more details : http://www.cs.utoronto.ca/~dianeh/tutorials/params/

这篇关于写/读任何对象/于Android内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆