ObjectInputStream无法按预期工作

时间:2013-12-16 17:21:47

标签: java android ioexception

我试图将对象保存到文件并再次阅读,这是我正在使用的代码:

此代码将对象存储到文件中:(在onDestroy中调用)

    private void serializeModulesManager() {
        try {
            FileOutputStream fos = openFileOutput("modules.txt",
                    Context.MODE_PRIVATE);
            ObjectOutputStream out = new ObjectOutputStream(fos);
            out.writeObject(Module.values());
            out.flush();
            out.close();
            fos.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

此代码用于从文件中读取对象:(在onCreate()中调用)

private void restoreModulesManager() {
        FileInputStream fileIn;
        Module[] arr = null;
        try {

            fileIn = openFileInput("modules.txt");
            ObjectInputStream in = new ObjectInputStream(fileIn); 

            arr = (Module[]) in.readObject(); // "in" object is empty so I cant read anything from it.. I get an EOFException and get to IOException

            for (Module c : arr) {
                Module.valueOf(c.name()).serilize(c);
            }

            in.close();
            fileIn.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (StreamCorruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

问题在于restoreModulesManager()函数:当我从任务管理器关闭应用程序或重启设备时," ObjectInputStream在"对象是空的,所以我不能在下一行读取任何内容..

知道为什么会这样吗?

堆栈跟踪:

12-16 20:53:40.395: W/System.err(18512): java.io.EOFException
12-16 20:53:40.415: W/System.err(18512):    at java.io.DataInputStream.readByte(DataInputStream.java:98)
12-16 20:53:40.415: W/System.err(18512):    at java.io.ObjectInputStream.nextTC(ObjectInputStream.java:506)
12-16 20:53:40.425: W/System.err(18512):    at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:778)
12-16 20:53:40.425: W/System.err(18512):    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2006)
12-16 20:53:40.435: W/System.err(18512):    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1963)
12-16 20:53:40.435: W/System.err(18512):    at com.technion.coolie.CoolieActivity.restoreModulesManager(CoolieActivity.java:489)
12-16 20:53:40.445: W/System.err(18512):    at com.technion.coolie.CoolieActivity.onCreate(CoolieActivity.java:52)
12-16 20:53:40.445: W/System.err(18512):    at com.technion.coolie.skeleton.MainActivity.onCreate(MainActivity.java:28)
12-16 20:53:40.445: W/System.err(18512):    at android.app.Activity.performCreate(Activity.java:5250)
12-16 20:53:40.445: W/System.err(18512):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
12-16 20:53:40.455: W/System.err(18512):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2210)
12-16 20:53:40.455: W/System.err(18512):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2296)
12-16 20:53:40.455: W/System.err(18512):    at android.app.ActivityThread.access$700(ActivityThread.java:151)
12-16 20:53:40.455: W/System.err(18512):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
12-16 20:53:40.455: W/System.err(18512):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-16 20:53:40.455: W/System.err(18512):    at android.os.Looper.loop(Looper.java:137)
12-16 20:53:40.455: W/System.err(18512):    at android.app.ActivityThread.main(ActivityThread.java:5293)
12-16 20:53:40.465: W/System.err(18512):    at java.lang.reflect.Method.invokeNative(Native Method)
12-16 20:53:40.465: W/System.err(18512):    at java.lang.reflect.Method.invoke(Method.java:511)
12-16 20:53:40.465: W/System.err(18512):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
12-16 20:53:40.465: W/System.err(18512):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
12-16 20:53:40.465: W/System.err(18512):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

您不应该在onDestroy()中执行此操作,因为有些情况下永远不会调用onDestroy()。您应该在onPause()中执行此操作,因为这是唯一一个保证在您的应用不再位于前台时调用的生命周期事件。

此外,onDestroy()在生命周期的后期被调用,你不应该尝试在那里打开文件等事情。在onDestroy()中,您唯一应该做的就是释放已经分配的资源(内存,文件等),并确保关闭所有后台线程。