import java.io.*;
import java.util.*;
class Student implements Serializable
{
String name;
int age;
public Student(String name,int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String toString()
{
return name+" "+age;
}}public class TestObjectStream
{
public static void main(String[] args)
{
Student student [] = {
new Student("zhange",60),
new Student("zhansf",61),
new Student("zhagsg",62),
new Student("zangsh",63),
new Student("zfangi",65),
new Student("zhangg",66),
new Student("zhanga",67),
new Student("zhangb",68),
new Student("zhangc",69),
new Student("zhangd",70),
};
try{
File f=new File("f://haifei.txt"); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));for (int i = 0;i<student.length ;i++ )
{
oos.writeObject(student[i]);//写十个学生对象
}
oos.close();
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(f));
Object a = ois.readObject();
while (ois!=null)
{
System.out.println(a);
ois.close();
break;
}
}
catch(Exception e){
e.printStackTrace();
}
}
}为什么我读的时候只读出了一行...
import java.util.*;
class Student implements Serializable
{
String name;
int age;
public Student(String name,int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String toString()
{
return name+" "+age;
}}public class TestObjectStream
{
public static void main(String[] args)
{
Student student [] = {
new Student("zhange",60),
new Student("zhansf",61),
new Student("zhagsg",62),
new Student("zangsh",63),
new Student("zfangi",65),
new Student("zhangg",66),
new Student("zhanga",67),
new Student("zhangb",68),
new Student("zhangc",69),
new Student("zhangd",70),
};
try{
File f=new File("f://haifei.txt"); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(f));for (int i = 0;i<student.length ;i++ )
{
oos.writeObject(student[i]);//写十个学生对象
}
oos.close();
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(f));
Object a = ois.readObject();
while (ois!=null)
{
System.out.println(a);
ois.close();
break;
}
}
catch(Exception e){
e.printStackTrace();
}
}
}为什么我读的时候只读出了一行...
解决方案 »
- 询问专业培训学校的老师都没能解释清楚的问题,估计也是困扰很多新手的一个问题,有没有人能够解答一下呢
- java 字符串转日期
- java 怎么写啊!能不能附加代码和详细解释啊
- 问下大家都是怎么避免书写错误的?
- JFreeChart中的TimeSeries纵坐标问题
- float f=new Float(x);怎么不区分大小写?
- 要换工作了,心情不好,散分!为了女朋友愿意放弃这里的一切!无奈
- 一个关于applet 访问internet 网页的问题! 请各位高手相助!!!1
- 多个String 怎样合并?菜鸟马上给分!
- 关于Jbuilder5中的tomcat问题!
- 关于学习上有点迷茫问题--请教高手指教
- 小程序找错
try{
while(true){
a=ois.readObject();
System.out.println(a);
}
}
catch(Exception e){
ois.close();
}
可以读出所有的对象,在循环中读取对象,并利用异常结束循环
但最后抛出了异常..
请问那异常信息是什么意思
java.io.EOFException我应该怎么处理才可以不让他打印这种异常信息...
Object a;
while((a= ois.readObject())!=null)
{
System.out.println(a);
}
ois.close();
}
}
catch(EOFException e){
System.out.println("文件读取完毕");//处理里这步后跳出try块继续执行
}
catch(Exception e){
}