javaIO流序列化

class student implements Serializable{
String name;
int age;
static int num;
transient String sex;
student(){}
student(String name,int age,int num,String sex){
this.name=name;
this.age=age;
this.num=num;
this.sex=sex;
}
public String toString(){
return name+age+num+sex;
}
}
class FilesDemo2{
public static void main(String[] args){
try(FileOutputStream f = new FileOutputStream("D:\\java\\iotest\\Stream.txt");
ObjectOutputStream fo = new ObjectOutputStream(f);
FileInputStream f1 = new FileInputStream("D:\\java\\iotest\\Stream.txt");
ObjectInputStream fo1 = new ObjectInputStream(f1);
){
student s = new student("虞姬",18,45,"女");
fo.writeObject(s);
Object obi = fo1.readObject();
student s1 = (student) obi;
System.out.println(s1);
}catch(IOException|ClassNotFoundException e){
e.printStackTrace();
}
}

原文地址:https://www.cnblogs.com/01aa/p/6735694.html