许润卿 许润卿
关注数: 4 粉丝数: 151 发帖数: 398 关注贴吧数: 32
IO流异常 EOFException,我仅仅测试了一个writeObject.求大佬看眼 ackage com.njyb.io20200215;import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.EOFException; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class TestObjectIOStream { public static void main(String[] args) throws IOException, ClassNotFoundException{ //对象流可以存储在内存(字节数组流),文件,DATABASE中 //对象流(缓存流(输出流)) ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(baos)); //声明一个类,并写入字节数组流 A a = new A("乐乐",18); oos.writeObject(a); byte[] bb = baos.toByteArray(); oos.flush(); oos.close(); //对象流(缓存流(读取流(byte[]))) ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(bb))); Object obj = ois.readObject(); if(obj instanceof A) { A b = (A)obj; b.name(); System.out.println(b.getName()); } } } class A implements java.io.Serializable{ private String name; private int age; public A() { } public A(String name,int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public void name() { System.out.println("测试对象流"); }
1 下一页