【问题标题】:Reading my Serialized Object from File in Android在 Android 中从文件中读取我的序列化对象
【发布时间】:2011-05-31 14:33:27
【问题描述】:

这是我第一次尝试在任何平台上序列化/反序列化对象,委婉地说,我很困惑。

在我的游戏对象实现 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;
    }
          }

我从这里Android Java -- Deserializing a file on the Android platform 获得了此代码的基础,并尝试为我的应用程序修改它。跑步时我得到

 05-31 23:30:45.493: ERROR/copybit(1279): copyBits failed (Invalid argument)

何时应该加载输出并且保存的游戏从保存时开始。 任何帮助将不胜感激。

【问题讨论】:

    标签: android serialization file-io


    【解决方案1】:

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

    最后,将您的日志修改为如下所示:

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

    看看返回了什么错误。我预计序列化会以某种方式失败。

    【讨论】:

    • 我得到 06-01 00:07:41.293: ERROR/serializeObject(8453): java.io.FileNotFoundException: /mnt/sdcard/serializationtest 试图序列化。
    【解决方案2】:

    要对任何内容进行序列化或反序列化,您可以使用 SIMPLE api。这是非常容易使用。下载文件并在您的程序中使用它

    看这里

    http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#deserialize

    感谢迪帕克

    【讨论】:

      【解决方案3】:

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

      公共类 SerializeUtil {

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

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-19
        • 2015-04-03
        • 1970-01-01
        • 1970-01-01
        • 2013-05-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多