java序列化当中重写readObject当中为什么执行时出现异常?出现异常的地方在下面有标记....谢谢

importjava.io.*;classObjectSerialTest{publicstaticvoidmain(String[]args)throwsExcepti... import java.io.*;
class ObjectSerialTest
{
public static void main(String[] args) throws Exception
{
Employee e1=new Employee("zhangsan",25,3000.50);
Employee e2=new Employee("lisi",24,3100.40);
Employee e3=new Employee("wangwu",26,3800.60);

FileOutputStream fos=new FileOutputStream("employee.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(e1);
oos.writeObject(e2);
oos.writeObject(e3);
oos.close();

FileInputStream fis=new FileInputStream("employee.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
Employee e;
for(int i=0;i<3;i++)
{
e=(Employee) ois.readObject();//为什么这里会抛出异常无法正常执行?
System.out.println(e.name+" "+e.age+" "+e.salary);
}
ois.close();
}
}
class Employee implements Serializable
{
String name;
int age;
double salary;
transient Thread t=new Thread();
public Employee(String name,int age,double salary)
{
this.name=name;
this.age=age;
this.salary=salary;
}
private void writeObject(java.io.ObjectOutputStream oos)throws IOException
{
oos.writeInt(age);
oos.writeUTF(name);
System.out.println("write object");
}
private void readObeject(java.io.ObjectInputStream ois) throws IOException,ClassNotFoundException
{
age=ois.readInt();
name=ois.readUTF();
System.out.println("read object");
}

}
展开
 我来答
紫薇参星
科技发烧友

2011-12-04 · 有一些普通的科技小锦囊
知道大有可为答主
回答量:5983
采纳率:92%
帮助的人:3610万
展开全部
因为你readObject写成了readObeject,还有你没有写入和读出对象的salary属性,我给你加上了,你看看吧,运行没问题。完整的程序如下:
import java.io.*;
class ObjectSerialTest
{
public static void main(String[] args) throws Exception
{
Employee e1=new Employee("zhangsan",25,3000.50);
Employee e2=new Employee("lisi",24,3100.40);
Employee e3=new Employee("wangwu",26,3800.60);

FileOutputStream fos=new FileOutputStream("employee.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(e1);
oos.writeObject(e2);
oos.writeObject(e3);
oos.close();

FileInputStream fis=new FileInputStream("employee.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
Employee e;
for(int i=0;i<3;i++)
{
e=(Employee) ois.readObject();//为什么这里会抛出异常无法正常执行?
System.out.println(e.name+" "+e.age+" "+e.salary);
}
ois.close();
}
}
class Employee implements Serializable
{
String name;
int age;
double salary;
transient Thread t=new Thread();
public Employee(String name,int age,double salary)
{
this.name=name;
this.age=age;
this.salary=salary;
}
private void writeObject(java.io.ObjectOutputStream oos)throws IOException
{
oos.writeInt(age);
oos.writeUTF(name);
oos.writeDouble(salary);
System.out.println("write object");
}
private void readObject(java.io.ObjectInputStream ois) throws IOException,ClassNotFoundException
{
age=ois.readInt();
name=ois.readUTF();
salary=ois.readDouble();
System.out.println("read object");
}

}
运行结果:
write object
write object
write object
read object
zhangsan 25 3000.5
read object
lisi 24 3100.4
read object
wangwu 26 3800.6
天佑笔记
2011-12-04 · TA获得超过433个赞
知道小有建树答主
回答量:352
采纳率:0%
帮助的人:213万
展开全部
FileInputStream fis=new FileInputStream("employee.txt");
ObjectInputStream ois=new ObjectInputStream(fis);
Employee e;

你在这个fis读取这个流时,那个文件是空的。
所以在取得第一行的时候没有取到值。就报错了
invalid type code: 67
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式