对象流操作的除了基本数据类型、字符串,还包括其他的各种对象,包括自定义的对象。
ObjectInputStream和ObjectOutputStream有特殊的叫法。
序列化:ObjectOutputStream,对象输出流。
反序列化:ObjectInputStream,对象输入流。
必须先写出后读取,写出的顺序与输出的顺序保持一致。
不是所有的对象都可以序列化,必须有一个标识,告诉虚拟机这个对象可以序列化。
ObjectOutputStream将原始的数据类型和对象写入字节流OutputStream,然后通过ObjectInputStream读取、还原(重构)对象。
只有支持java.io.Serializable接口的对象才能写入流中。每个可序列化对象的类被编码。
java.io.Serializable接口就像一个通行证,有了它表示可以序列化,它什么都没有:
package java.io;
public interface Serializable {
}
ObjectInputStream 反序列化先前使用ObjectOutputStream编写的原始数据和对象。
字符串实现了java.io.Serializable接口,可以被序列化:
public final class String
implements java.io.Serializable, Comparable, CharSequence,
Constable, ConstantDesc {......}
Date类也实现了java.io.Serializable接口,可以被序列化:
public class Date
implements java.io.Serializable, Cloneable, Comparable
{......}
序列化对象举例:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
public class Test {
public static void main(String[] args) throws ClassNotFoundException {
//写出
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new BufferedOutputStream(baos));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
//操作数据类型+数据
oos.writeUTF("这是字符串");
oos.writeInt(18);
oos.writeBoolean(false);
oos.writeChar('A');
//对象
oos.writeObject("这是一个String类型的对象");
oos.writeObject(new Date());
Employee emp = new Employee("小张", 5000);
oos.writeObject(emp);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}
byte[] datas = baos.toByteArray();
//读取
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
} catch (IOException e1) {
e1.printStackTrace();
}
//顺序与写出一致
String msg = null;
int age = 0;
boolean flag = true;
char ch = 0;
try {
msg = ois.readUTF();
age = ois.readInt();
flag = ois.readBoolean();
ch = ois.readChar();
System.out.println(msg);
System.out.println(age);
System.out.println(flag);
System.out.println(ch);
Object str = ois.readObject();
Object date = ois.readObject();
Object employee = ois.readObject();
if(str instanceof String) {
String strObj = (String)str;
System.out.println(strObj);
}
if(date instanceof Date) {
Date dateObj = (Date)date;
System.out.println(dateObj);
}
if(employee instanceof Employee) {
Employee empObj = (Employee)employee;
System.out.println(empObj.getName() + "-->" + empObj.getSalary());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//javabean 封装数据
class Employee implements java.io.Serializable{
private String name;
private double salary;
public Employee() {
super();
}
public Employee(String name, double salary) {
super();
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
输出结果:
这是字符串
18
false
A
这是一个String类型的对象
Wed Jul 29 18:53:03 GMT+08:00 2020
小张-->5000.0
如果有数据不想让它被序列化,比如敏感数据,可以加关键字transient :
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Test {
public static void main(String[] args) throws ClassNotFoundException {
//写出 --> 序列化
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new BufferedOutputStream(baos));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
Employee emp = new Employee("小张", 5000);
oos.writeObject(emp);
oos.flush();
} catch (IOException e) {
e.printStackTrace();
}
byte[] datas = baos.toByteArray();
//读取 --> 反序列化
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
} catch (IOException e1) {
e1.printStackTrace();
}
try {
//对象的数据还原
Object employee = ois.readObject();
if(employee instanceof Employee) {
Employee empObj = (Employee)employee;
System.out.println(empObj.getName() + "-->" + empObj.getSalary());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
//javabean 封装数据
class Employee implements java.io.Serializable{
private transient String name;//该数据不需要序列化
private double salary;
public Employee() {
super();
}
public Employee(String name, double salary) {
super();
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
输出结果:
null-->5000.0
name的数据被抹掉了。
序列化写到文件里,然后从文件中读出,反序列化:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;
public class Test {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//写出 --> 序列化
ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("obj.ser")));
//操作数据类型+数据
oos.writeUTF("这是字符串");
oos.writeInt(18);
oos.writeBoolean(false);
oos.writeChar('A');
//对象
oos.writeObject("这是一个String类型的对象");
oos.writeObject(new Date());
Employee emp = new Employee("小张", 5000);
oos.writeObject(emp);
oos.flush();
oos.close();
//读取 --> 反序列化
ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("obj.ser")));
//对象的数据还原
String msg = ois.readUTF();
int age = ois.readInt();
boolean flag = ois.readBoolean();
char ch = ois.readChar();
System.out.println(msg);
System.out.println(age);
System.out.println(flag);
System.out.println(ch);
Object str = ois.readObject();
Object date = ois.readObject();
Object employee = ois.readObject();
if(str instanceof String) {
String strObj = (String)str;
System.out.println(strObj);
}
if(date instanceof Date) {
Date dateObj = (Date)date;
System.out.println(dateObj);
}
if(employee instanceof Employee) {
Employee empObj = (Employee)employee;
System.out.println(empObj.getName() + "-->" + empObj.getSalary());
}
ois.close();
}
}
//javabean 封装数据
class Employee implements java.io.Serializable{
private transient String name;//该数据不需要序列化
private double salary;
public Employee() {
super();
}
public Employee(String name, double salary) {
super();
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}
输出结果:
这是字符串
18
false
A
这是一个String类型的对象
Wed Jul 29 19:46:49 GMT+08:00 2020
null-->5000.0
创建的文件:
序列化也有另外一个名称,叫做持久化,它可以存起来。