Java-装饰流-对象流 - 序列化和反序列化

ObjectInputStream(反序列化) & ObjectOutputStream(序列化)

1.先写出后读取
2.读取的顺序必须保持一致
3.不是所有的对象都能序列化,要加上serializable接口才行

当不想对对象中的某个属性序列化时,在属性中添加transient就行啦~
[eg]:private transient String name;

4.序列化有另外一个名称叫做持久化

(1)序列化到字节数据(不需要关闭流)

package cn.lesson.Burrfed;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;


/**
 * 数据流
 * 1.先写出后读取
 * 2.读取和写出的顺序一致
 * 3.不是所有的对象都可以序列化,必须要使用serializable接口
 * @author MacBook
 *
 */
public class DataTest {

	public static void main(String[] args) throws IOException, ClassNotFoundException {
		//选择流
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ObjectOutputStream dos = new ObjectOutputStream(new BufferedOutputStream(baos));
		//写出
		dos.writeUTF("啦啦啦啦");
		dos.writeInt(12);
		dos.writeBoolean(false);
		dos.writeChar('a');
		
		//对象
		dos.writeObject("谁解其中味");
		dos.writeObject(new Date());
		Employee emp = new Employee("马云", 400);
		dos.writeObject(emp);
				
		dos.flush();
		
		byte[] datas = baos.toByteArray();
		System.out.println(datas.length);
		
		//读取
		
		//选择流
		ObjectInputStream dis = 
				new ObjectInputStream(
						new BufferedInputStream(
								new ByteArrayInputStream(datas)));
		
		String msg = dis.readUTF();
		int age = dis.readInt();
		boolean i = dis.readBoolean();
		char ch = dis.readChar();
		
		Object str = dis.readObject();
		Object date = dis.readObject();
		Object employee = dis.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());
			
		}
		
		
		
		System.out.println(ch);
		
		
	}

}

/**
 * javabean 封装数据
 * @author MacBook
 *
 */
class Employee implements java.io.Serializable{
	private String name;
	private double salary;
	
	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;
	}
	
	
}

(2)序列化到文件中

package cn.lesson.Burrfed;

import java.io.BufferedInputStream;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Date;


/**
 * 数据流
 * 1.先写出后读取
 * 2.读取和写出的顺序一致
 * 3.不是所有的对象都可以序列化,必须要使用serializable接口
 * @author MacBook
 *
 */
public class DataTest {

	public static void main(String[] args) throws IOException, ClassNotFoundException {
		//选择流
		
		ObjectOutputStream dos = new ObjectOutputStream(new FileOutputStream("obj.txt"));
		//写出
		dos.writeUTF("啦啦啦啦");
		dos.writeInt(12);
		dos.writeBoolean(false);
		dos.writeChar('a');
		
		//对象
		dos.writeObject("谁解其中味");
		dos.writeObject(new Date());
		Employee emp = new Employee("马云", 400);
		dos.writeObject(emp);
				
		dos.flush();
		dos.close();
		
		//读取
		
		//选择流
		ObjectInputStream dis = 
				new ObjectInputStream(
						new BufferedInputStream(
								new FileInputStream("obj.txt")));
		
		String msg = dis.readUTF();
		int age = dis.readInt();
		boolean i = dis.readBoolean();
		char ch = dis.readChar();
		
		Object str = dis.readObject();
		Object date = dis.readObject();
		Object employee = dis.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());
			
		}
		
		
		
		dis.close();
		
		
	}

}

/**
 * javabean 封装数据
 * @author MacBook
 *
 */
class Employee implements java.io.Serializable{
	private transient String name;
	private double salary;
	
	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;
	}
	
	
}


猜你喜欢

转载自blog.csdn.net/qq_43287650/article/details/83826242