* java默认序列化
* 1.实现Serializable接口(约定)
* 2.序列化和反序列化
* 3.实现java对象和字节序列的转换
* 4.将对象的字节序列(内存)持久化到磁盘(通常为文件),高并发session处理(减轻内存压力)
* 5.网络传输对象的字节序列,两个进程实现远程网络通信,(所有数据类型,都以二进制序列形式在网络上传送(接受发送))
* 6.实现Serializable接口的类采用默认的序列化方式 。* 7.静态变量(类变量)和transient修饰变量(不序列化属性修饰符),不被序列化,解看下测试:
其他:java序列化2[实现Externalizable接口,可控序列化]
实现序列化接口Serializable的实体类代码:
package com.pakege.b;
import java.io.Serializable;
/**
* java默认序列化
* 1.实现Serializable接口(约定)
* 2.序列化和反序列化
* 3.实现java对象和字节序列的转换
* 4.将对象的字节序列(内存)持久化到磁盘(通常为文件),高并发session处理(减轻内存压力)
* 5.网络传输对象的字节序列,两个进程实现远程网络通信,(所有数据类型,都以二进制序列形式在网络上传送(接受发送))
* 6.实现Serializable接口的类采用默认的序列化方式
* */
public class SerializableTest implements Serializable{
/**
* 序列化ID
* */
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;
//getter and setter
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 static void setCardNo(String cardNo) {
SerializableTest.cardNo = cardNo;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public static String getCardNo() {
return cardNo;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
序列化测试代码:
package com.pakege.b;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import org.junit.Test;
public class Obj2BinaryAtDisk {
@Test
public void testObj2BinaryAtDisk(){
//准备序列化对象
SerializableTest customer = new SerializableTest();
customer.setId(55555);
customer.setName("Tony");
customer.setAge("24");
customer.setGender("男");
customer.setBalance(9876543210.25d);
//ObjectOutputStream对象输出流,完成对象到字节序列的转换(序列化)
ObjectOutputStream out = null;
try {
out = new ObjectOutputStream(new FileOutputStream(new File("D:/XXXXX/Customer.txt")));
//序列化(对象写入流字节序列中[写出实现Serializable接口对象的字段,不实现不写出])
out.writeObject(customer);
System.out.println("Obj2BinaryAtDisk序列化成功!");
} catch (FileNotFoundException e) {
System.out.println("序列化对象到磁盘文件,FileNotFoundException:"+e.getMessage());
} catch (IOException e) {
System.out.println("序列化对象到磁盘文件,IOException:"+e.getMessage());
}finally{
try {
out.close();
} catch (IOException e) {
System.out.println("序列化对象到磁盘文件,输出对象关闭失败,IOException:"+e.getMessage());
}
}
}
}
序列化结果:
Obj2BinaryAtDisk序列化成功!

反序列化测试代码:
package com.pakege.b;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.text.MessageFormat;
import org.junit.Test;
/**
* cardNo为静态变量(类变量,类所有),不能被初始化
* 对象都在堆,而静态变量在方法区
* 序列化是对对象的操作
* 静态会随着类的加载,进行初始化一次
* */
public class Binary2ObjAtMemory {
@Test
public void testBinary2ObjAtMemory(){
ObjectInputStream input = null;
try {
input = new ObjectInputStream(new FileInputStream(new File("D:/XXXXX/Customer.txt")));
//反序列化(磁盘字节序列读入[实现Serializable接口对象,按序列化顺序反序列化并封装对象返回])
SerializableTest customer = (SerializableTest) 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()));
System.out.println("Obj2BinaryAtDisk序列化成功!");
} catch (FileNotFoundException e) {
System.out.println("反序列化到内存对象,FileNotFoundException:"+e.getMessage());
} catch (ClassNotFoundException e) {
System.out.println("反序列化到内存对象,ClassNotFoundException:"+e.getMessage());
} catch (IOException e) {
System.out.println("反序列化到内存对象,IOException:"+e.getMessage());
}finally{
try {
input.close();
} catch (IOException e) {
System.out.println("反序列化到内存对象,输入对象关闭失败,IOException:"+e.getMessage());
}
}
}
}
反序列化结果:
id=55,555,name=Tony,age=24,balance=9,876,543,210.25,cardNo=0102221992212**53,gender=null
Obj2BinaryAtDisk序列化成功!
注:静态变量(类变量)和transient修饰变量(不序列化属性修饰符),不被序列化,静态看下图(序列化是对'对象'的序列化):

解:没有静态属性在对象customer中,静态属性cardNo的值来自类型class加载静态初始化。