关闭

测试对象流的操作

304人阅读 评论(0) 收藏 举报
分类:

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;
}


}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:24287次
    • 积分:738
    • 等级:
    • 排名:千里之外
    • 原创:46篇
    • 转载:36篇
    • 译文:0篇
    • 评论:3条
    文章分类
    最新评论