这是我正在序列化的文件Employee.java:
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Date;
public class Employee implements Externalizable {
String lName;
String fName;
double salary;
String address;
Date hireDate;
int id;
@Override
public void writeExternal(ObjectOutput out) throws IOException {
// TODO Auto-generated method stub
out.writeDouble(salary);
out.writeInt(id);
}
@Override
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
// TODO Auto-generated method stub
salary= in.readDouble();
id= in.readInt();
}
}
这是我实际序列化的代码
import java.io.*;
import java.util.Date;
public class ClassA {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee emp= new Employee();
emp.salary= 2000;
emp.id= 234;
FileOutputStream fos= null;
ObjectOutput oos= null;
try{
fos= new FileOutputStream("C:\\dkbexternalizable.ser");
oos= new ObjectOutputStream(fos);
// oos.writeObject(emp);
emp.writeExternal(oos);
oos.flush();
}
catch(IOException e){
try {
oos.close();
fos.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
System.out.println("The employee object has been serialized into "+"C:\\dkbexternalizable.ser");
}
}
当我调试上面的序列化代码时,我看到0.0和0是为.ser文件中的salary和id值传递的。 我希望将2000和234传递给他们。 有一些基本的错误,我无法弄清楚。有人可以帮帮我吗?