zoukankan      html  css  js  c++  java
  • java ObjectOutputStream

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    
    class Employee {
        private String name;
    
        Employee(String name) {
            this.name = name;
        }
    
        @Override
        public String toString() {
            return name;
        }
    }
    
    class SerEmployee extends Employee implements Serializable {
        SerEmployee(String name) {
            super(name);
        }
    }
    
    public class SerializationDemo {
        public static void main(String[] args) {
            ObjectOutputStream oos = null;
            ObjectInputStream ois = null;
            try {
                oos = new ObjectOutputStream(new FileOutputStream("employee.dat"));
                SerEmployee se = new SerEmployee("John Doe");
                System.out.println(se);
                oos.writeObject(se);
                oos.close();
                oos = null;
                System.out.println("se object written to file");
                ois = new ObjectInputStream(new FileInputStream("employee.dat"));
                se = (SerEmployee) ois.readObject();
                System.out.println("se object read from file");
                System.out.println(se);
            } catch (ClassNotFoundException cnfe) {
                cnfe.printStackTrace();
            }
    
            catch (IOException ioe) {
                ioe.printStackTrace();
            } finally {
                if (oos != null)
                    try {
                        oos.close();
                    } catch (IOException ioe) {
                        assert false; // shouldn't happen in this context
                    }
                if (ois != null)
                    try {
                        ois.close();
                    } catch (IOException ioe) {
                        assert false; // shouldn't happen in this context
                    }
            }
        }
    }
  • 相关阅读:
    docker 加速器配置目录
    php 超时设置笔记
    php socket通过smtp发送邮件(纯文本、HTML,多收件人,多抄送,多密送)
    fabric 安装
    centos7下使用yum安装pip
    【转】linux tar 压缩
    ASP.NET MVC 5 默认模板的JS和CSS 是怎么加载的?
    NHibernate with ASP.NET MVC 入门示例
    Ajax入门
    NHibernate入门
  • 原文地址:https://www.cnblogs.com/rojas/p/5407548.html
Copyright © 2011-2022 走看看