Student.txt
Test.txt
package zhang;
import java.io.Serializable;
public class Student implements Serializable {
/**
*
*/
private static final long serialVersionUID = -4502633718082654465L;
private String name;
private int age;
public static String schoolName;//学校名称
private transient String pwd; //属性的值将不再被序列化
public Student() {
super();
}
public Student(String name, int age, String pwd) {
super();
this.name = name;
this.age = age;
this.pwd = pwd;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + ", pwd=" + pwd + "]"+"schoolName="+schoolName;
}
}
package zhang;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class TEST {
public static void write() throws Exception, IOException
{
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("E://Data.txt"));
Student s = new Student("zhang",20,"121");
s.schoolName="BJ";
oos.writeObject(s);
oos.close();
}
public static void read() throws Exception, IOException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("E:\\Data.txt"));
Student s = (Student)ois.readObject();
ois.close();
System.out.println(s);
}
public static void main(String[] args) throws IOException, Exception {
write();
read();
}
}

老师,我这个不知道哪里错了,静态的schoolName 有显示