zoukankan      html  css  js  c++  java
  • 通过序列化复制整个对象

    用到的所有的类都要实现Serializable接口

    public class Address implements Serializable{
    
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private String state;
        private String province;
        private String city;
        public Address(String s,String p,String c){
            this.state = s;
            this.province = p;
            this.city = c;
        }
        
        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append("地区:"+state+"
    ");
            sb.append("省份:"+province+"
    ");
            sb.append("城市:"+city+"
    ");
            return sb.toString();
        }
    
        /**
         * @return the state
         */
        public String getState() {
            return state;
        }
    
        /**
         * @param state the state to set
         */
        public void setState(String state) {
            this.state = state;
        }
    
        /**
         * @return the province
         */
        public String getProvince() {
            return province;
        }
    
        /**
         * @param province the province to set
         */
        public void setProvince(String province) {
            this.province = province;
        }
    
        /**
         * @return the city
         */
        public String getCity() {
            return city;
        }
    
        /**
         * @param city the city to set
         */
        public void setCity(String city) {
            this.city = city;
        }
        
        
    }
    public class Employee implements Serializable{
    
        private String name;
        private int age;
        private Address address;
        public Employee(String n,int a,Address ad){
            this.name = n;
            this.age = a;
            this.address = ad;
            
        }
        /**
         * @return the name
         */
        public String getName() {
            return name;
        }
        /**
         * @param name the name to set
         */
        public void setName(String name) {
            this.name = name;
        }
        /**
         * @return the age
         */
        public int getAge() {
            return age;
        }
        /**
         * @param age the age to set
         */
        public void setAge(int age) {
            this.age = age;
        }
        /**
         * @return the address
         */
        public Address getAddress() {
            return address;
        }
        /**
         * @param address the address to set
         */
        public void setAddress(Address address) {
            this.address = address;
        }
        /* (non-Javadoc)
         * @see java.lang.Object#toString()
         */
        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            sb.append("姓名:"+name);
            sb.append("年龄:"+age);
            sb.append("地址:"+"
    "+address+"
    ");
            return sb.toString();
        }
        
    }

    复制整个对象

        public static void main(String[] args) {
            System.out.println("序列化前");
            Address address = new Address("中国","吉林","长春");
            Employee employee1 = new Employee("张XX",30,address);
            System.out.println(employee1);
            System.out.println("序列化后:");
            ObjectOutputStream out = null;
            ObjectInputStream  in = null;
            Employee employee2 = null;
            try {
                out = new ObjectOutputStream(new FileOutputStream("emploooo.dat"));
                out.writeObject(employee1);
                in = new ObjectInputStream(new FileInputStream("emploooo.dat"));
                employee2 = (Employee)in.readObject();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }catch (IOException e) {
                e.printStackTrace();
            }catch (ClassNotFoundException e) {
                e.printStackTrace();
            }finally{
                //toDo释放资源
            }
            System.out.println(employee2);
        
        }
    }

  • 相关阅读:
    redis持久化的方式RDB 和 AOF
    centos7搭建mysql-5.7.22主从复制
    Vue项目上线后刷新报错404问题(apache,nginx,tomcat)
    Zabbix监控华为交换机
    Zabbix数据库清理历史数据
    MySQL增删改查基本语句
    什么是SQL注入式攻击?
    .NET面试题(二)
    .NET面试题(一)
    .NET面试题(三)
  • 原文地址:https://www.cnblogs.com/lonely-buffoon/p/5572638.html
Copyright © 2011-2022 走看看