Java IO_对象流 尚学堂185

对象流操作的除了基本数据类型、字符串,还包括其他的各种对象,包括自定义的对象。

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<String>, CharSequence,
               Constable, ConstantDesc {......}

Date类也实现了java.io.Serializable接口,可以被序列化:

public class Date
    implements java.io.Serializable, Cloneable, Comparable<Date>
{......}

序列化对象举例:

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

创建的文件: 

  这不是给人看的,是给机器阅读的。

序列化也有另外一个名称,叫做持久化,它可以存起来。


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值