向/从Android内存写入/读取任何对象

时间:2012-06-26 15:55:52

标签: java android

我目前正在写方法:

  • 从内存中读取对象
  • 将对象写入内存

我试图保存一个String对象并再次读取它,但我的代码不起作用。

这是我的代码:

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();

        }

    }

2 个答案:

答案 0 :(得分:0)

您写道:

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

但是object只是传递给函数的参数的副本。您可以在方法中更改该副本(就像您一样),但这对实际参数没有影响。

你必须改回你的对象:

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;
}

阅读本文了解更多详情:http://www.cs.utoronto.ca/~dianeh/tutorials/params/

答案 1 :(得分:0)

这是任何感兴趣的人的工作代码:

注意:使用readObjectFromMemory(String filename, Object object)时,您必须投射返回的对象。

/**
     * <b><i>public void writeObjectToMemory(String filename, Object object)</i></b>
     * <br>
     * Since: API 1
     * <br>
     * <br>
     * Write a object to the phone's internal storage.
     * 
     * @param filename 
     * The name of the file you wish to write to.
     *
     *      
     */

    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();
            this.gameEngineLog.d(classTAG, "Object successfully written: " + filename);
        } 
        catch (FileNotFoundException e) {
            e.printStackTrace();
            this.gameEngineLog.d(classTAG, "Object couldn't be opened: " + filename);

        } 
        catch (IOException e) {
            e.printStackTrace();
            this.gameEngineLog.d(classTAG, "Object couldn't be opened: " + filename);

        }

    }

    /**
     * <b><i>public Object readObjectFromMemory(String filename, Object object)</i></b>
     * <br>
     * Since: API 1
     * <br>
     * <br>
     * Read a object from the phone's internal storage.
     * 
     * @param filename 
     * The name of the file you wish to read from.
     *
     *      
     */

    public Object readObjectFromMemory(String filename, Object object) {
        Object defautObject = null;
        FileInputStream fis;
        try {
            fis = game.openFileInput(filename);
            ObjectInputStream is = new ObjectInputStream(fis);
            defautObject = is.readObject();
            is.close();
            this.gameEngineLog.d(classTAG, "Object successfully read: " + filename);
        } 
        catch (FileNotFoundException e) {
            e.printStackTrace();
            this.gameEngineLog.d(classTAG, "Object couldn't be opened: " + filename);

        } 
        catch (StreamCorruptedException e) {
            e.printStackTrace();
            this.gameEngineLog.d(classTAG, "Object couldn't be opened: " + filename);

        } 
        catch (IOException e) {
            e.printStackTrace();
            this.gameEngineLog.d(classTAG, "Object couldn't be opened: " + filename);

        } 
        catch (ClassNotFoundException e) {
            e.printStackTrace();
            this.gameEngineLog.d(classTAG, "Object couldn't be opened: " + filename);

        }

        return defautObject;

    }