java对象流ObjectStream

来源:互联网 发布:js 24小时时间插件 编辑:程序博客网 时间:2024/05/01 07:33

package com.wj.objectstream;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
 *
 * @author wJing
 * time:2014-2-18 22:54:59
 * ObjectStream
 */
public class ObjectStreamDemo1 {

    public static void main(String[] args) throws Exception {
        writeObject();
        //readObject();
    }
    
    public static void writeObject() throws FileNotFoundException, IOException {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("d:\\obj.txt"));
        oos.writeObject(new Person("zhangsan",30));
        oos.close();
    }
    
    public static void readObject() throws FileNotFoundException, IOException, Exception {
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("d:\\obj.txt"));
        Person p = (Person)ois.readObject();
        System.out.println(p.toString());
        ois.close();
    }

}

class Person implements Serializable {
    private String person;
    private int age;
    public Person(String person, int age) {
        this.age = age;
        this.person = person;
    }
    @Override
    public String toString() {
        return "Person [person=" + person + ", age=" + age + "]";
    }
    
}
0 0
原创粉丝点击