测试对象流的操作

import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.Serializable;


public class TestObjectInputStream {


public static void main(String[] args)throws Exception {
Dog dog1=new Dog("小黑",3);
Dog dog2=new Dog("小黄",2);
ObjectOutputStream oos=new ObjectOutputStream(
new FileOutputStream(
new File("C:\\Documents and Settings\\Administrator\\桌面\\dxz.txt")));
oos.writeObject(dog1);
oos.writeObject(dog2);
oos.close();
ObjectInputStream ois=new ObjectInputStream (
new FileInputStream(
new File ("C:\\Documents and Settings\\Administrator\\桌面\\dxz.txt")));
dog1=(Dog)ois.readObject();
dog2=(Dog)ois.readObject();
System.out.println(dog1.name);
System.out.println(dog2.name);
ois.close();



}

}
class Dog implements Serializable{
String name;
int age;
public Dog(String name, int age) {
super();
this.name = name;
this.age = age;
}


}

阅读更多
个人分类: java
上一篇学生成绩的录入排列
下一篇测试数据流的操作
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭