在Android中从文件读取我的序列化对象

在Android中从文件读取我的序列化对象,android,serialization,file-io,Android,Serialization,File Io,这是我第一次尝试在任何平台上序列化/反序列化对象,说得客气一点,我感到困惑 在对我的游戏对象实现Serializable后,我将其输出到一个文件,因此: public void saveGameState(){ ByteArrayOutputStream bos = new ByteArrayOutputStream(); try { ObjectOutput out = new ObjectOutputStream(bos);

这是我第一次尝试在任何平台上序列化/反序列化对象,说得客气一点,我感到困惑

在对我的游戏对象实现Serializable后,我将其输出到一个文件,因此:

public void saveGameState(){


    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

    try { 


        ObjectOutput out = new ObjectOutputStream(bos); 
          out.writeObject(theGame);//theGame is an instance of the custom class                  
                                          //TGame which stores game info.

          byte[] buf = bos.toByteArray(); 

          FileOutputStream fos = this.openFileOutput(filename,                      
                   Context.MODE_PRIVATE);
          fos.write(buf);
          fos.close(); 
        } catch(IOException ioe) { 
          Log.e("serializeObject", "error", ioe); 


        } 
        File f =this.getDir(filename, 0);
        Log.v("FILE",f.getName());    
}
这似乎是可行的,因为我没有得到任何例外。我只能确定何时反序列化它。这就是事情发展的方向

public God loadSavedGame(){
    TGame g=null;
    InputStream instream = null;
    try {
        instream = openFileInput(filename);
    } catch (FileNotFoundException e) {

        e.printStackTrace();
        return null;
    }
     try {
        ObjectInputStream ois = new ObjectInputStream(instream);

         try {
            g= (TGame) ois.readObject();
            return g;
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
     } catch (StreamCorruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
          }
我从这里得到了这段代码的基础,并试图为我的应用程序修改它。当我跑步的时候

 05-31 23:30:45.493: ERROR/copybit(1279): copyBits failed (Invalid argument)
当输出应该加载,并且保存的游戏从保存时开始启动。
任何帮助都将不胜感激。

要序列化或反序列化您可以使用的任何简单api。它很容易使用。下载该文件并在程序中使用

看看这里


感谢Deepak

您显示的错误与序列化无关:它实际上是一个视频显示错误。我建议在序列化之前查看对象,以确保其不为null,我还建议序列化到SD卡上的一个文件,以确保您确实有数据输出,因此在调试时使用new FileOutputStream/mnt/sdcard/serializationtest作为输出流,使用new FileInputStream/mnt/sdcard/serializationtest作为输入流;工作后,您可以切换回上下文方法,但请确保在执行此操作时插入SD卡

最后,修改日志,使其如下所示:

try {
        ObjectInputStream ois = new ObjectInputStream(instream);

         try {
            g= (TGame) ois.readObject();
            return g;
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            android.util.Log.e("DESERIALIZATION FAILED (CLASS NOT FOUND):"+e.getMessage(), e);
            e.printStackTrace();
            return null;
        }
     } catch (StreamCorruptedException e) {
            android.util.Log.e("DESERIALIZATION FAILED (CORRUPT):"+e.getMessage(), e);
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    } catch (IOException e) {
            android.util.Log.e("DESERIALIZATION FAILED (IO EXCEPTION):"+e.getMessage(), e);
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

并查看返回了什么错误。我希望序列化会以某种方式失败。

我创建了下面的类来执行保存和检索对象

公共类序列化util{

public static <T extends Serializable>   void saveObjectToFile(Context context, T object, String fileName){

    try {

        FileOutputStream fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
        ObjectOutputStream os = new ObjectOutputStream(fos);
        os.writeObject(object);
        os.close();
        fos.close();

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


public static<T extends Serializable> T getObjectFromFile(Context context, String fileName){

    T object = null;

    try {

        FileInputStream fis = context.openFileInput(fileName);
        ObjectInputStream is = new ObjectInputStream(fis);
        object = (T) is.readObject();
        is.close();
        fis.close();

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

    return object;
}

public static void removeSerializable(Context context, String filename) {
    context.deleteFile(filename);
}
}


我得到06-01 00:07:41.293:ERROR/serializeObject8453:java.io.FileNotFoundException:/mnt/sdcard/serializationtest,正在尝试序列化。