如何判断ObjectOutputStream已经结束?
import java.io.FileInputStream;
import
java.io.FileNotFoundException;
import java.io.FileOutputStream;
import
java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class ObjectOutputStreamu {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream( "a.txt ");
ObjectOutputStream oos = new ObjectOutputStream(fos);
Employee e1 = new Employee(30, "jack ", 100);
Employee e2 = new Employee(30, "jack ", 99.999999);
Employee e3 = new Employee(30, "jack ", 99.999998);
oos.writeObject(e1);
oos.writeObject(e2);
oos.writeObject(e3);
oos.close();
FileInputStream fis = new FileInputStream( "a.txt ");
ObjectInputStream ois = new ObjectInputStream(fis);
Object o;
while((o = ois.readObject()) != null) { // 这个判断怎么没有起到作用?最后还是跳出异常 if (o instanceof Employee)
System.out.println(((Employee)o).name + ": " + ((Employee)o).age + ": " + ((Employee)o).score); }
} catch (
FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (
IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (
ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Employee implements Serializable
{
int age;
String name;
double score;
public Employee(int age, String name, double score)
{
this.age = age;
this.name = name;
this.score = score;
}
public Employee() {
}
}
------解决方案--------------------判断流到头 的结果返回的是-1不是null
------解决方案--------------------while(ois.available()> 0)