内部类上的java-IO
ObjectOutputStream/ObjectInputStream是否可以作为内部类?我可以正常编写它,并检查创建的文件,但当我尝试使用ObjectInputStream将其读回时,仅在尝试读取对象o=oos.readObject()时,我得到一个EOFEException内部类上的java-IO,java,io,objectinputstream,eofexception,Java,Io,Objectinputstream,Eofexception,ObjectOutputStream/ObjectInputStream是否可以作为内部类?我可以正常编写它,并检查创建的文件,但当我尝试使用ObjectInputStream将其读回时,仅在尝试读取对象o=oos.readObject()时,我得到一个EOFEException 我使用相同的文件对象打开两个流,所以这不是问题所在 它似乎与内部类的性质无关——只有公共int的类与更复杂的类一样失败 我必须继续前进,创建一个常规类,并在sender类中实例化,但我讨厌离开,不知道这是否可能,如果不
希望这有助于节省一些时间。能否提供一点示例代码?最明显的解释是,在尝试读回输出流之前,您没有关闭/刷新输出流。能否包含一点示例代码?最明显的解释是,在尝试读回输出流之前,您没有关闭/刷新输出流。这一点对我很有用。请查看您的解决方案是否存在任何差异 public class Example implements Serializable { public static void main(String[] args) throws IOException, ClassNotFoundException { new Example().run(); } private void run() throws IOException, ClassNotFoundException { Inner inner = new Inner(); inner.x = 5; ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream outputStream = new ObjectOutputStream( out ); outputStream.writeObject( inner ); ByteArrayInputStream in = new ByteArrayInputStream( out.toByteArray() ); ObjectInputStream inputStream = new ObjectInputStream( in ); Inner inner2 = (Inner) inputStream.readObject(); System.out.println( inner2.x ); } class Inner implements Serializable { int x; } } 公共类示例实现了可序列化{ 公共静态void main(字符串[]args)引发IOException,ClassNotFoundException{ 新示例().run(); } private void run()引发IOException,ClassNotFoundException{ 内部=新内部(); 内部x=5; ByteArrayOutputStream out=新建ByteArrayOutputStream(); ObjectOutputStream outputStream=新的ObjectOutputStream(输出); outputStream.writeObject(内部); ByteArrayInputStream in=新的ByteArrayInputStream(out.toByteArray()); ObjectInputStream inputStream=新ObjectInputStream(in); 内部inner2=(内部)inputStream.readObject(); System.out.println(inner2.x); } 类内部实现可序列化{ int x; } }
这个适合我。请查看您的解决方案是否存在任何差异 public class Example implements Serializable { public static void main(String[] args) throws IOException, ClassNotFoundException { new Example().run(); } private void run() throws IOException, ClassNotFoundException { Inner inner = new Inner(); inner.x = 5; ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream outputStream = new ObjectOutputStream( out ); outputStream.writeObject( inner ); ByteArrayInputStream in = new ByteArrayInputStream( out.toByteArray() ); ObjectInputStream inputStream = new ObjectInputStream( in ); Inner inner2 = (Inner) inputStream.readObject(); System.out.println( inner2.x ); } class Inner implements Serializable { int x; } } 公共类示例实现了可序列化{ 公共静态void main(字符串[]args)引发IOException,ClassNotFoundException{ 新示例().run(); } private void run()引发IOException,ClassNotFoundException{ 内部=新内部(); 内部x=5; ByteArrayOutputStream out=新建ByteArrayOutputStream(); ObjectOutputStream outputStream=新的ObjectOutputStream(输出); outputStream.writeObject(内部); ByteArrayInputStream in=新的ByteArrayInputStream(out.toByteArray()); ObjectInputStream inputStream=新ObjectInputStream(in); 内部inner2=(内部)inputStream.readObject(); System.out.println(inner2.x); } 类内部实现可序列化{ int x; } }
你说的内部类是什么意思?(如果你指的是内部类,那么你可以序列化它们,但是规范告诉你不要这样做。)你能提供一些细节吗?在哪里,为什么?谢谢。你说的内部课程是什么意思?(如果你指的是内部类,那么你可以序列化它们,但是规范告诉你不要这样做。)你能提供一些细节吗?在哪里,为什么?谢谢。输出流已关闭。代码在一个封闭的网络机器上,因此可以转录,但代码并不简单。我不清楚为什么不关闭流在这里是适用的——如果流没有关闭,那么当使用外部类时问题也会出现,不是吗?输出流是关闭的。代码在一个封闭的网络机器上,因此可以转录,但代码并不简单。我不清楚为什么不关闭流会适用于这里-如果流没有关闭,那么当使用外部类时问题也会出现,不是吗?谢谢-我的问题是“可能吗”-答案显然是肯定的。你对TomHawlin关于规范中说不要序列化内部类的评论有什么看法吗?谢谢-我的问题是“可能吗”-答案显然是肯定的。你对Tom Hawlin关于规范中说不要序列化内部类的评论有什么看法吗?