为什么ObjectOutputStream.writeObject()抛出IOException? [英] Why does ObjectOutputStream.writeObject() throw IOException?

查看:1725
本文介绍了为什么ObjectOutputStream.writeObject()抛出IOException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个Android应用程序,基于特定事件自动改变手机的状态,如电池电量。我有一个有3个主要参数,名称,事件和状态(事件和状态是2等自定义类)配置文件类

在我ProfileEdit类,有各种组件如的EditText和微调为用户配置一个配置文件。我需要访问类型的配置文件的ArrayList中显示每个配置ListActivity类,我ProfileEdit类获取当前的ArrayList,并增加了新的配置文件,或者更新的。

我不得不这样做的想法首先是,使用ObjectOutputStream我的ArrayList写手机SD卡,那么就可以通过任何需要它的类进行检索。但是我试图做到这一点,到目前为止只落得抛出IOException异常。我的code是如下:

 的ObjectOutputStream OOS =新的ObjectOutputStream(新的FileOutputStream(/ SD卡/ profile.prof));
oos.writeObject(配置文件);
oos.flush();
oos.close();

的轮廓目的是我的ArrayList要写入到文件中。我想不通这是为什么抛出异常。我试图通过调用的writeObject(你好),改变对象的字符串;而不再抛出异常,所以我只能假设它与ArrayList的一个问题。我是ArrayList类实现Serializable和我的个人资料类也实现序列化的IM pression下,虽然它做同样的事情没有。

另外,如果有更好的办法,我可以给我的两个ProfileList文件(ListActivity)和ProfileEdit类访问同一个ArrayList中,那么请让我知道。

编辑:这里的IO异常堆栈跟踪:(该ProfileEdit类114线是的writeObject()线)

  EZSettings(14788):IO异常
EZSettings(14788):java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1535)
EZSettings(14788):java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
EZSettings(14788):java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
EZSettings(14788):java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
EZSettings(14788):java.util.ArrayList.writeObject(ArrayList.java:651)
EZSettings(14788):java.lang.reflect.Method.invokeNative(本机方法)
EZSettings(14788):java.lang.reflect.Method.invoke(Method.java:507)
EZSettings(14788):java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1219)
EZSettings(14788):java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575)
EZSettings(14788):java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
EZSettings(14788):java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
EZSettings(14788):java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
EZSettings(14788):com.ezstatechanger.ProfileEdit.saveState(ProfileEdit.java:114)
EZSettings(14788):com.ezstatechanger.ProfileEdit.access $ 0(ProfileEdit.java:80)
EZSettings(14788):$ com.ezstatechanger.ProfileEdit 1.onClick(ProfileEdit.java:72)
EZSettings(14788):android.view.View.performClick(View.java:2538)
EZSettings(14788):$ android.view.View PerformClick.run(View.java:9152)
EZSettings(14788):android.os.Handler.handleCallback(Handler.java:587)
EZSettings(14788):android.os.Handler.dispatchMessage(Handler.java:92)
EZSettings(14788):android.os.Looper.loop(Looper.java:123)
EZSettings(14788):android.app.ActivityThread.main(ActivityThread.java:3691)
EZSettings(14788):java.lang.reflect.Method.invokeNative(本机方法)
EZSettings(14788):java.lang.reflect.Method.invoke(Method.java:507)
EZSettings(14788):$ com.android.internal.os.ZygoteInit MethodAndArgsCaller.run(ZygoteInit.java:847)
EZSettings(14788):com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
EZSettings(14788):dalvik.system.NativeStart.main(本机方法)


解决方案

你的所有对象及其所有字段需要被序列 - 否则他们不能被序列化。 IO异常的另一个来源是缺乏权限SD卡上写的。
(除了做你的应用程序需要有人这个数据吗?如果不是,它属于成provate区)。

序列化配置数据很方便,但可能是坏主意 - 如果你的对象签名的变化,数据将无法读取。更好的解决办法是马歇尔对象成JSON
并保存此文件(无耻的自我广告上: https://github.com/ko5tik/jsonserializer

I'm currently working on an Android app that automatically changes the phones state based on a certain event, such as battery level. I have a Profile class which has 3 main parameters, names, Event and State (Event and State are 2 other custom classes)

In my ProfileEdit class, there are various components such as EditText and Spinner for users to configure a Profile. I need access to an ArrayList of type Profile in a ListActivity class which displays each profile, and my ProfileEdit class which gets the current ArrayList and adds the new profile, or updates one.

The idea I had to do this first was to use ObjectOutputStream to write my ArrayList to the phones sd card, then it can be retrieved by any class that needs it. However my attempts to do this so far just end up throwing an IOException. My code is as follows:

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("/sdcard/profile.prof"));
oos.writeObject(profiles);
oos.flush();
oos.close();

The profiles object is my ArrayList to be written to the file. I can't figure out why this is throwing the exception. I tried changing the object to a string by calling writeObject("Hello"); and that no longer threw the exception, so I can only assume its a problem with the ArrayList. I was under the impression that the ArrayList class implements serializable and my Profile class also implements serializable, although it does the same thing without.

Alternatively, if there's a better way I can give both my ProfileList (ListActivity) and ProfileEdit classes access to the same ArrayList then please let me know.

Edit: Here's the IO Exceptions stack trace: (Line 114 of the ProfileEdit class is the writeObject() line)

EZSettings(14788): IO Exception
EZSettings(14788): java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1535)
EZSettings(14788): java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
EZSettings(14788): java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
EZSettings(14788): java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
EZSettings(14788): java.util.ArrayList.writeObject(ArrayList.java:651)
EZSettings(14788): java.lang.reflect.Method.invokeNative(Native Method)
EZSettings(14788): java.lang.reflect.Method.invoke(Method.java:507)
EZSettings(14788): java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1219)
EZSettings(14788): java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1575)
EZSettings(14788): java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1847)
EZSettings(14788): java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1689)
EZSettings(14788): java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1653)
EZSettings(14788): com.ezstatechanger.ProfileEdit.saveState(ProfileEdit.java:114)
EZSettings(14788): com.ezstatechanger.ProfileEdit.access$0(ProfileEdit.java:80)
EZSettings(14788): com.ezstatechanger.ProfileEdit$1.onClick(ProfileEdit.java:72)
EZSettings(14788): android.view.View.performClick(View.java:2538)
EZSettings(14788): android.view.View$PerformClick.run(View.java:9152)
EZSettings(14788): android.os.Handler.handleCallback(Handler.java:587)
EZSettings(14788): android.os.Handler.dispatchMessage(Handler.java:92)
EZSettings(14788): android.os.Looper.loop(Looper.java:123)
EZSettings(14788): android.app.ActivityThread.main(ActivityThread.java:3691)
EZSettings(14788): java.lang.reflect.Method.invokeNative(Native Method)
EZSettings(14788): java.lang.reflect.Method.invoke(Method.java:507)
EZSettings(14788): com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
EZSettings(14788): com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
EZSettings(14788): dalvik.system.NativeStart.main(Native Method)

解决方案

All your objects and all their fields need to be serializable - otherwise they can not be serialized. Another source of IO exception would be lack of permission to write on SD card. ( does somebody except your application needs this data? if not, it belongs into provate area.)

Serializing config data is convenient, but may be bad idea - if your object signature changes, data will be unreadable. Better solution would be to marshall objects into JSON and save this file ( Shameless self advertising on: https://github.com/ko5tik/jsonserializer )

这篇关于为什么ObjectOutputStream.writeObject()抛出IOException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆