serialization DEMO

来源:互联网 发布:淘宝运营考核指标 编辑:程序博客网 时间:2024/06/05 19:41
package HeadFirstJava;import java.io.*;@SuppressWarnings("serial")public class Box implements Serializable {    @SuppressWarnings("unused")    private int width;    @SuppressWarnings("unused")    private int height;        public void setWidth(int w) {        width = w;    }    public void setHeight(int h) {        height = h;    }    public static void main(String[] args) {        Box myBox = new Box();        myBox.setWidth(50);        myBox.setHeight(20);                try {            FileOutputStream fs = new FileOutputStream("foo.ser");            ObjectOutputStream os = new ObjectOutputStream(fs);            os.writeObject(myBox);            os.close();        } catch(Exception ex) {            ex.printStackTrace();        }    }}

0 0
原创粉丝点击