package Zjshuchu;
import java.io.Serializable;
public class Dog implements Serializable{
private static final long serialVersionUID = 45156465448L;
private String name;
private transient String color;
public String getName(){
return name;
}
public Dog(String name, String color) {
super();
this.name = name;
this.color = color;
}
public Dog() {
super();
}
@Override
public String toString() {
return "Dog [name=" + name + ", color=" + color + "]";
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
}
package Zjshuchu; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class Demo011 { public static void main(String[] args) throws IOException { //注意:序列化必须走的一部是必须实现序列化接口 // 1、确定目的地 FileOutputStream fos=new FileOutputStream("d:\java\dog.txt"); //2、创建对象输出流 ObjectOutputStream oos=new ObjectOutputStream(fos); //3.创建对象 Dog d=new Dog(); d.setName("java"); d.setColor("white"); //4.将对象写入文件 oos.writeObject(d); //5、释放资源 oos.close(); } }
package Zjshuchu; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.ObjectInputStream; public class Demo002 { public static void main(String[] args) throws IOException, ClassNotFoundException { FileInputStream fis=new FileInputStream("d:\java\dog.txt"); ObjectInputStream ois=new ObjectInputStream(fis); Dog d=(Dog)ois.readObject(); System.out.println(d); } }