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");
}
} 展开
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");
}
} 展开
2个回答
展开全部
因为你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
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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询