0

我目前正在写以下方法:

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

我试图保存一个 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();

        }

    }
4

2 回答 2

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/

于 2012-06-26T16:06:05.920 回答
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;

    }
于 2012-06-26T16:19:57.273 回答