关于ObjectInputStream的问题 用什么样的方法能实现用ObjectInputStream中的readObject()循环的从文件中读取数据请举例详细说明 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Student stu1 = new Student(20,"zhangsan",25,"huaxue");Student stu2 = new Student(18,"lisi",23,"wuli");Student stu = null; try{ FileOutputStream fos = new FileOutputStream("Student.txt"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(stu1); oos.writeObject(stu2); fos.close(); oos.close(); FileInputStream fis = new FileInputStream("Student.txt"); ObjectInputStream ois = new ObjectInputStream(fis); while((stu = (Student)ois.readObject())!=null) { stu = (Student)ois.readObject(); System.out.println("Id: " + stu.getId()); System.out.println("Name: " + stu.getName()); System.out.println("Age: " + stu2.getAge()); System.out.println("Department: " + stu2.getDepartment()); fis.close(); ois.close(); }}catch(EOFException e){ e.printStackTrace();}程序运行的时候在while((stu = (Student)ois.readObject())!=null)出现错误只能将第一第记录读出Exception in thread "main" java.io.IOException: No such file or directory at java.io.FileInputStream.read(Native Method) at java.io.ObjectInputStream$PeekInputStream.peek(ObjectInputStream.java:2200) at java.io.ObjectInputStream$BlockDataInputStream.peek(ObjectInputStream.java:2490) at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2500) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1267) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:339) at Serialization.main(Serialization.java:24) System.out.println( "Age: " + stu.getAge()); System.out.println( "Department: " + stu.getDepartment()); 使用数据库连接池方式编码的时候为什么要close() 银行管理系统中的代码问题 数据转换的疑问! 求winsock 实现的ftp源码,谢谢大家! 左边jtree右边jtable的资源管理器问题 JAVA中的转义字符问题。 请问如何给文件夹改名? 数组问题 各位大侠,请教如何隐藏窗体? 谁知道哪里还有像CSDN这样国内大的技术论坛?太感谢了 hibernate映射必须要设置OID,难度硬要在数据库表中加一个id吗? 序列化的问题
Student stu2 = new Student(18,"lisi",23,"wuli");
Student stu = null;
try
{
FileOutputStream fos = new FileOutputStream("Student.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(stu1);
oos.writeObject(stu2);
fos.close();
oos.close();
FileInputStream fis = new FileInputStream("Student.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
while((stu = (Student)ois.readObject())!=null)
{
stu = (Student)ois.readObject();
System.out.println("Id: " + stu.getId());
System.out.println("Name: " + stu.getName());
System.out.println("Age: " + stu2.getAge());
System.out.println("Department: " + stu2.getDepartment());
fis.close();
ois.close();
}
}
catch(EOFException e)
{
e.printStackTrace();
}
程序运行的时候在while((stu = (Student)ois.readObject())!=null)出现错误
只能将第一第记录读出
Exception in thread "main" java.io.IOException: No such file or directory
at java.io.FileInputStream.read(Native Method)
at java.io.ObjectInputStream$PeekInputStream.peek(ObjectInputStream.java:2200)
at java.io.ObjectInputStream$BlockDataInputStream.peek(ObjectInputStream.java:2490)
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:2500)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1267)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:339)
at Serialization.main(Serialization.java:24)
System.out.println( "Department: " + stu.getDepartment());