* java可控制对象序列化:选择对象属性序列化,可序列化时加入当前类外其他属性
* 1.实现Externalizable接口(此接口实现了Serializable接口约定)
* 2.序列化和反序列化
* 3.控制序列化就是有选择的序列化对象(属性)
* 4.在序列化过程中储存(序列化),其他非this对象包含的数据或this信息
* 5.实现java对象和字节序列的转换
* 6.将对象的字节序列(内存)持久化到磁盘(通常为文件),高并发session处理(减轻内存压力)
* 7.网络传输对象的字节序列,两个进程实现远程网络通信,(所有数据类型,都以二进制序列形式在网络上传送(接受发送))
* 8.实现Serializable接口的类采用默认的序列化方式
* 9.静态变量(类变量)和transient修饰变量(不序列化属性修饰符),不被序列化
解,见上片:java序列化1[实现Serializable接口]
实现Externalizable接口的实体类代码:
package com.pakege.b; import java.io.Externalizable; import java.io.IOException; import java.io.ObjectInput; import java.io.ObjectOutput; import java.util.Date; /** * java可控制对象序列化:选择对象属性序列化,可序列化时加入当前类外其他属性 * 1.实现Externalizable接口(此接口实现了Serializable接口约定) * 2.序列化和反序列化 * 3.控制序列化就是有选择的序列化对象(属性) * 4.在序列化过程中储存(序列化),其他非this对象包含的数据或this信息 * 5.实现java对象和字节序列的转换 * 6.将对象的字节序列(内存)持久化到磁盘(通常为文件),高并发session处理(减轻内存压力) * 7.网络传输对象的字节序列,两个进程实现远程网络通信,(所有数据类型,都以二进制序列形式在网络上传送(接受发送)) * 8.实现Serializable接口的类采用默认的序列化方式 * 9.静态变量(类变量)和transient修饰变量(不序列化属性修饰符),不被序列化 * */ public class ExternalizableTest implements Externalizable{ private static final long serialVersionUID = -4036467956431644864L; private int id; private String name; private String age; private double balance; //静态变量,不能被序列化 private static String cardNo = "0102221992212**53"; //transient修饰变量,不能被序列化 private transient String gender; /** * writerExternal方法在序列化时被调用 * 在该方法中控制序列化内容 * 选择性序列化(属性) * */ @Override public void writeExternal(ObjectOutput out) throws IOException { System.out.println("序列化writeExternal执行……"); String addStr = "非this对象包含的数据"; Date date = new Date(); out.writeObject(date); System.out.println(date); out.writeObject(addStr); //序列化this的name和balance out.writeObject(this.getName()); out.writeObject(this.getBalance()); } /** * readExternal方法在反序列时被调用 * 在该方法中控制反序列的内容 * 按序列化顺序反序列化并封装对象返回 * */ @Override public void readExternal(ObjectInput in) throws IOException,ClassNotFoundException { System.out.println("反序列化readExternal执行……"); Date datei = (Date) in.readObject(); String addi = (String) in.readObject(); System.out.println(datei+"<->"+addi); this.setName((String)in.readObject()); this.setBalance((double)in.readObject()); } //getter and setter public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public static String getCardNo() { return cardNo; } public static void setCardNo(String cardNo) { ExternalizableTest.cardNo = cardNo; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } }
测试代码:
package com.pakege.b; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.text.MessageFormat; import org.junit.Test; public class ExternalTest { //序列化 @Test public void testO2B() throws FileNotFoundException, IOException{ //准备序列化对象 ExternalizableTest customer = new ExternalizableTest(); customer.setId(55555); customer.setName("Tony"); customer.setAge("24"); customer.setGender("男"); customer.setBalance(9876543210.25d); //序列化一个ExternalizableTest对象customer ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(new File("D:/XXXXX/Customere.txt"))); out.writeObject(customer); out.close(); } //反序列化 @Test public void testB2O() throws FileNotFoundException, IOException, ClassNotFoundException{ //反序列化 ObjectInputStream input = new ObjectInputStream(new FileInputStream(new File("D:/XXXXX/Customere.txt"))); ExternalizableTest customer = (ExternalizableTest) input.readObject(); System.out.println(MessageFormat.format("id={0},name={1},age={2},balance={3},cardNo={4},gender={5}", customer.getId(), customer.getName(),customer.getAge(),customer.getBalance(),customer.getCardNo(),customer.getGender())); } }
测试结果:
序列化writeExternal执行……
Tue Jul 14 14:20:09 CST 2015
反序列化readExternal执行……
Tue Jul 14 14:20:09 CST 2015<->非this对象包含的数据
id=0,name=Tony,age=null,balance=9,876,543,210.25,cardNo=0102221992212**53,gender=null