ObjectInputStream和ObjectOutputStream
注:
- 不是所有的对象都可以序列化,必须实现接口serializable
- 先写出再读取
- 读取顺序和写出顺序一致
- 基本数据类型可以直接保留下来,引用类型需要自己反序列化后强转
序列化: 将数据结构或对象转换成二进制串的过程
反序列化:将在序列化过程中所生成的二进制串转换成数据结构或者对象的过程
序列化:ObjectOutputStream——对象输出流
对象输出到字节流里,存储到(文件,数据库,内存——字节数组)
将java对象的原始数据类型和对象写入OutputStream,可以使用ObjectOutputStream读取(重构)对象,可以通过使用流的文件来实现对象的持久存储
其中void writeObject(object obj)——将指定的对象写入ObjectOutputStream
反序列化:ObjectInputStream——对象输入流
把序列化后的对象(文件,数据库,内存——字节数组)还原成对象
其中object readObject()——从ObjectInputStream读取一个对象
具体示例——写入字节数组中
注:transient 作为关键字(透明),可以隐藏不想被序列化的属性
package com.io.cx3;
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;
/**
* 对象流:
* 1、写出后读取
* 2、读取的顺序与写出保持一致
* 3、不是所有的对象都可以序列化Serializable
*
* ObjectOutputStream
* ObjectInputStream
*/
public class object1 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//写出 -->序列化
ByteArrayOutputStream baos =new ByteArrayOutputStream();
ObjectOutputStream oos =new ObjectOutputStream(new BufferedOutputStream(baos));
//操作数据类型 +数据
oos.writeUTF("混合");
oos.writeInt(18);
oos.writeBoolean(false);
oos.writeChar('a');
//对象——字符串也属于对象
oos.writeObject("时代");
oos.writeObject(new Date());
Employee emp =new Employee("代红",001);//自定义对象
oos.writeObject(emp);
oos.flush();
byte[] datas =baos.toByteArray();
System.out.println("数据的大小为:"+datas.length);
//读取 -->反序列化
ObjectInputStream ois =new ObjectInputStream(new BufferedInputStream(new ByteArrayInputStream(datas)));
//顺序与写出一致
String msg = ois.readUTF();
int age = ois.readInt();
boolean flag = ois.readBoolean();
char ch = ois.readChar();
System.out.println(flag);
//对象的数据还原
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());
}
}
}
//javabean 封装数据
class Employee implements java.io.Serializable{
//必须要加入接口 Serializable
private transient String name; //该数据不需要序列化
private double salary;
public Employee() {
}
public Employee(String name, double salary) {
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;
}
}
具体示例——写入文件中
过程概述是:通过程序将要序列化的内容写入一个txt文件中,生成给机器看的文字存储在txt文件中,在程序中用反序列化来得到原本的内容,并显示在终端上
package com.io.cx3;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
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 object2 {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//写出 -->序列化
ObjectOutputStream oos =new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("dest.txt")));
//操作数据类型 +数据
oos.writeUTF("混合");
oos.writeInt(18);
oos.writeBoolean(false);
oos.writeChar('a');
//对象——字符串也属于对象
oos.writeObject("时代");
oos.writeObject(new Date());
Employee emp =new Employee("代红",1);//自定义对象
oos.writeObject(emp);
oos.flush();
oos.close();
//读取 -->反序列化
ObjectInputStream ois =new ObjectInputStream(
new BufferedInputStream(new FileInputStream("dest.txt")));
//顺序与写出一致
String msg = ois.readUTF();
int age = ois.readInt();
boolean flag = ois.readBoolean();
char ch = ois.readChar();
System.out.println(flag);
//对象的数据还原
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 Employees) {
Employees empObj = (Employees) employee;
System.out.println(empObj.getName()+"-->"+empObj.getSalary());
}
ois.close();
}
}
//javabean 封装数据
class Employees implements java.io.Serializable{
//必须要加入接口 Serializable
private transient String name; //该数据不需要序列化
//效果是:null-->1.0
private double salary;
public Employees() {
}
public Employees(String name, double salary) {
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;
}
}