zoukankan      html  css  js  c++  java
  • Java对象序列化和反序列

    package xml;
    
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    import java.util.UUID;
    
    public class Ser {
        public static void main(String[] args) throws Exception {
            Student student = new Student();
            student.setStuName(UUID.randomUUID().toString());
            student.setId(1);
            student.setStuAge(22);
            byte[] tmp = toByte(student);
            Student student2 = tooT(Student.class, tmp);
            System.out.println(student2.getStuName());
        }
    
        /**
         * 序列化
         * 
         * @param t
         * @return
         */
        public static <T> byte[] toByte(T t) {
            ByteArrayOutputStream b = new ByteArrayOutputStream();
            ObjectOutputStream o = null;
            try {
                o = new ObjectOutputStream(b);
                o.writeObject(t);
                return b.toByteArray();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (b != null) {
                    try {
                        b.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    if (o != null) {
                        try {
                            o.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                }
            }
            return null;
    
        }
    
        /**
         * 反序列
         * 
         * @param clazz
         * @param buffer
         * @return
         * @throws Exception
         */
        @SuppressWarnings("unchecked")
        public static <T> T tooT(Class<T> clazz, byte[] buffer) throws Exception {
            ByteArrayInputStream i = new ByteArrayInputStream(buffer);
            ObjectInputStream o = null;
            try {
                o = new ObjectInputStream(i);
                return (T) o.readObject();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (i != null) {
                    i.close();
                }
                if (o != null) {
                    o.close();
                }
            }
            return null;
    
        }
    }
    
    class Student implements Serializable {
        /**
         * 
         */
        private static final long serialVersionUID = 5723299859036666342L;
        private int id;
        private String stuName;
        private int stuAge;
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getStuName() {
            return stuName;
        }
    
        public void setStuName(String stuName) {
            this.stuName = stuName;
        }
    
        public int getStuAge() {
            return stuAge;
        }
    
        public void setStuAge(int stuAge) {
            this.stuAge = stuAge;
        }
    
        public static long getSerialversionuid() {
            return serialVersionUID;
        }
    
    }
  • 相关阅读:
    java学习笔记3
    java学习笔记 2
    linux用户登录指定目录
    [Jenkins 新插件] 兼容阿里开发手册 (P3C) 的火线插件安装使用教程
    Color a Tree HDU
    网络流初步
    K度限制MST poj 1639
    曼哈顿距离MST
    ACM Amman Collegiate Programming Contest(7.22随机组队娱乐赛)
    HDU Distinct Values
  • 原文地址:https://www.cnblogs.com/mature1021/p/10446267.html
Copyright © 2011-2022 走看看