package shi;
import java.io.*;
import java.util.*;
public class duixangIO {
public static void main(String[] args) {
FileOutputStream fos = null;
FileInputStream fis = null;
ObjectOutputStream oos=null;
ObjectInputStream ois=null;
// 对象流依赖文件流,只有文件流才能打开与文件的操作
File f=new File("F:/test/javakc.txt");
try {
fos=new FileOutputStream(f);
fis=new FileInputStream(f);
oos=new ObjectOutputStream(fos);
ois=new ObjectInputStream(fis);
// 向文件中写入日期对象,直接以对象的形式写入,Object形式接受
Date date=new Date();
oos.writeObject(date);
duixiangTest t1=new duixiangTest();
duixiangTest t2=new duixiangTest();
t1.setName("小EZ");
t1.setSex("男");
t1.setAge(20);
t2.setName("小VN");
t2.setSex("女");
t2.setAge(20);
oos.writeObject(t2);
oos.writeObject(t1);
// 向文件中读取日期对象,先以Object类型接受,在转换为原本的内容
Object o=ois.readObject();
Date d=(Date)o;
System.out.println(d);
Object o1=ois.readObject();
duixiangTest s1=(duixiangTest)o1;
System.out.println(s1);
Object o2=ois.readObject();
duixiangTest s2=(duixiangTest)o2;
System.out.println(s2);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(oos!=null){
try {
oos.close();
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
//
//package shi;
//
//import java.io.Serializable;
//
//public class duixiangTest implements Serializable{
//
// private String name;
// private String sex;
// private int age;
// public void setName(String name) {
// this.name = name;
// }
// public void setSex(String sex) {
// this.sex = sex;
// }
// public void setAge(int age) {
// this.age = age;
// }
// @Override
// public String toString() {
// return "duixiangTest [name=" + name + ", sex=" + sex + ", age=" + age
// + "]";
// }
//
//
//
//}