序列化、反序列化代码示例

1、序列化、反序列化接口

/** * 序列化、反序列化接口 */ public interface ISerialization { // 序列化 byte[] serializer(T obj); // 反序列化 T deSerializer(byte[] data,Class clazz); }

2、实现自己定义的接口
/** * 序列化 反序列化 操作 * 序列化就是:程序运行必定在内存中; * 从正在运行的程序中,即内存中将数据; * 序列化 便于存储到磁盘(硬盘)中, * 或者便于网络传输到其他机器上 * 反序列化:将磁盘(硬盘)中,序列化好的内容,读取到正在运行的程序中,即内存 */ public class JavaSerializer implements ISerialization { @Override public byte[] serializer(T obj) { ObjectOutputStream objectOutputStream=null; try { objectOutputStream=new ObjectOutputStream(new FileOutputStream(new File("user"))); objectOutputStream.writeObject(obj); }catch (Exception e){ e.printStackTrace(); }finally { if(objectOutputStream!=null){ try{ objectOutputStream.close(); }catch (Exception e){ e.printStackTrace(); } } } return null; }@Override public T deSerializer(byte[] data, Class clazz) { ObjectInputStream objectInputStream=null; try { objectInputStream =new ObjectInputStream(new FileInputStream(new File("user"))); return (T)objectInputStream.readObject(); }catch (Exception e){ e.printStackTrace(); }finally { if(objectInputStream!=null){ try { objectInputStream.close(); }catch (Exception e){ e.printStackTrace(); }} }return null; } }

3、实体类
注意; 1、Serializable必须得实现java自带的序列化接口 2、使用IDEA 自带的 serialVersionUID ,不能删除 ;删除不能发序列成功
public class Userimplements Serializable{ // 序列化值不能删除 ,如果删除 ,那么反序列化就不能把文件读取出去 private static final long serialVersionUID = -2236182038380256247L; private String name; private int age; public String getName() { return name; }public void setName(String name) { this.name = name; }public int getAge() { return age; }public void setAge(int age) { this.age = age; }public User(String name, int age) { this.name = name; this.age = age; }public User() { }@Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }

序列化、反序列化代码示例
文章图片
Image.png 序列化、反序列化代码示例
文章图片
Image2.png 4、测试
public class Demo { public static void main(String[] args) { // ----------注释start1 ISerialization iSerialization=new JavaSerializer(); User user=new User(); user.setAge(18); user.setName("Mic"); byte[] serialByte=iSerialization.serializer(user); // 序列化 // ----------注释end1 // 运行 之后 产生一个user文件 User dUser=iSerialization.deSerializer(serialByte,User.class); // 反序列化 System.out.println(dUser); }}

运行结果
产生一个user文件;
User{name='Mic', age=18}

我们把User中的 private static final long serialVersionUID = -2236182038380256247L; 注释掉
然后改写Demo中的方法
public static void main(String[] args) { ISerialization iSerialization=new JavaSerializer(); byte[] serialByte= file2byte("E:/MyCode-Gupao/serialization/user"); User dUser=iSerialization.deSerializer(serialByte,User.class); System.out.println(dUser); }// 读取文件成字节数组 public static byte[] file2byte(String path) { try { FileInputStream in =new FileInputStream(new File(path)); //当文件没有结束时,每次读取一个字节显示 byte[] data=https://www.it610.com/article/new byte[in.available()]; in.read(data); in.close(); return data; } catch (Exception e) { e.printStackTrace(); return null; } }

运行之后,报错
java.io.InvalidClassException: serialization.User; local class incompatible: stream classdesc serialVersionUID = -2236182038380256247, local class serialVersionUID = -3132643898329134254 at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:687) at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1876) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1745) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2033) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1567) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:427) at serialization.JavaSerializer.deSerializer(JavaSerializer.java:104) at serialization.Demo.main(Demo.java:30) null

所以如果使用IDEA 的serialVersionUID 不能删除
我们也可以将上面的第1步保留,其他的都重新改写
2、 实现自己定义的接口
public class JavaSerializer implements ISerialization { @Override public byte[] serializer(T obj) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = null; try { objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(obj); return byteArrayOutputStream.toByteArray(); } catch (IOException e) { e.printStackTrace(); } finally { if (objectOutputStream != null) { try { objectOutputStream.close(); } catch (Exception e) { e.printStackTrace(); } try { byteArrayOutputStream.close(); } catch (IOException e) { e.printStackTrace(); }} } return null; }@Override public T deSerializer(byte[] data, Class clazz) { ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data); ObjectInputStream objectInputStream = null; try { objectInputStream = new ObjectInputStream(byteArrayInputStream); return (T)objectInputStream.readObject(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { if (objectInputStream != null) { try { objectInputStream.close(); } catch (Exception e) { e.printStackTrace(); } try { objectInputStream.close(); } catch (IOException e) { e.printStackTrace(); }} } return null; } }

3、实体类
public class Userimplements Serializable{ private String name; private int age; public String getName() { return name; }public void setName(String name) { this.name = name; }public int getAge() { return age; }public void setAge(int age) { this.age = age; }public User(String name, int age) { this.name = name; this.age = age; }public User() { }@Override public String toString() { return "User{" + "name='" + name + '\'' + ", age=" + age + '}'; } }

序列化、反序列化代码示例
文章图片
这里写图片描述
序列化、反序列化代码示例
文章图片
这里写图片描述 4、测试类
public class Demo { public static void main(String[] args) { ISerialization iSerialization=new JavaSerializer(); User user=new User(); user.setAge(18); user.setName("Mic"); byte[] serialByte=iSerialization.serializer(user); // 运行 之后 产生一个user文件User dUser=iSerialization.deSerializer(serialByte,User.class); System.out.println(dUser); }

【序列化、反序列化代码示例】运行结果
User{name='Mic', age=18}

    推荐阅读