简体   繁体   English

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

[英]Write/Read any object to/from Android memory

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. 我试图保存一个String对象并再次读取它,但是我的代码无法正常工作。

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. 但是object只是您传递给函数的参数的副本。 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/ 请阅读以获取更多详细信息: http : //www.cs.utoronto.ca/~dianeh/tutorials/params/

Here's working code for anybody that is interested: 这是任何有兴趣的人的工作代码:

Note: When using the readObjectFromMemory(String filename, Object object) you will have to cast the returned object. 注意:使用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;

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM