Java IO流之对象流与序列化

版权声明:LemonSnm https://blog.csdn.net/LemonSnm/article/details/89843650

对象流:

 ObjectOutputStream:将Java对象的基本数据类型和图像写入OutputStream

ObjectInputStream:对以前使用ObjectOutputStream写入的基本数据和对象进行反序列化。

 序列化: 

序列化一组对象可采用对象数组的形式,因为对象数组可以向Object进行转型操作

如果一个类创建的对象,需要被序列化。那么该类必须实现Serializable接口
Serializable是一个标记接口,没有任何定义,为了告诉JVM该类对象可以被序列化 

对象序列化过程:

把对象写入文件:实际写入的是类名、属性名、属性类型、属性值等 

 反序列化过程:

 从文件中把对象的内容读取出来,还原成对象 

 什么时候对象需要被序列化?

1、把对象保存到文件中(存储到物理介质)

2、对象需要在网络上传输 

 需要序列化的student对象:

package com.lemon;

import java.io.Serializable;

/**
 * 如果一个类创建的对象,需要被序列化。那么该类必须实现Serializable接口
 * Serializable是一个标记接口,没有任何定义,为了告诉JVM该类对象可以被序列化
 * 
 * 
 * 什么时候对象需要被序列化?
 * 1、把对象保存到文件中(存储到物理介质)
 * 2、对象需要在网络上传输
 * 如果对象没有实现Serializable接口:报错:java.io.NotSerializableException
 * 
 * @author lemonSun
 * 2019年5月5日下午2:29:06
 */
public class Student implements Serializable {

	/**
	 * 编号
	 */
	private static final long serialVersionUID = 1L;
	private String name;
	private int age;
	private String sex;
	private transient int id; //在序列化时被忽略
	
	public Student() {
		super();
	}
	
	public Student(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}
	
	
	
}

实现序列化与反序列化: 

package com.lemon;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Arrays;
/**
 * 对象流和序列化
 * @author lemonSun
 *
 * 2019年5月5日下午2:26:06
 */
public class ObjectStreamDemo {

	public static void main(String[] args) {
		//序列化单个对象
//		writeObject();
//		readObject();
		//序列化多个对象
		writeObjects();
		readObjects();
		
	}
	
	/**
	 * 反序列化过程:
	 * 从文件中把对象的内容读取出来,还原成对象
	 */
	private static void readObject() {
		//目标文件
		File file = new File("F:\\javatest\\student.obj");
		try {
			//字节输入流
			InputStream in = new FileInputStream(file);
			//对象流
			ObjectInputStream oos = new ObjectInputStream(in);
			//写入输出Student对象
			Student student = (Student)oos.readObject();
			oos.close();
			System.out.println(student);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 对象序列化:
	 * 把对象写入文件:实际写入的是类名、属性名、属性类型、属性值等;
	 */
	private static void writeObject() {
	
		//需要序列化的对象
		Student student = new Student("张三",18,"男");
		//目标文件
		File file = new File("F:\\javatest\\student.obj");
		try {
			//字节输出流
			OutputStream out = new FileOutputStream(file);
			//对象流
			ObjectOutputStream oos = new ObjectOutputStream(out);
			//写入输出Student对象
			oos.writeObject(student);
			oos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 序列化多个对象:
	 * 1、把多个对象放到数组中
	 * 2、序列化数组
	 */
	private static void writeObjects() {
		
		//需要序列化的对象
		Student student1 = new Student("张三",18,"男");
		Student student2 = new Student("李四",19,"男");
		//全部放在数组中
		Student[] students = {student1,student2};
		//目标文件
		File file = new File("F:\\javatest\\students.obj");
		try {
			//字节输出流
			OutputStream out = new FileOutputStream(file);
			//对象流
			ObjectOutputStream oos = new ObjectOutputStream(out);
			//写入输出Student对象
			oos.writeObject(students);
			oos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 反序列化多个对象:
	 */
	private static void readObjects() {
		//目标文件
		File file = new File("F:\\javatest\\students.obj");
		try {
			//字节输入流
			InputStream in = new FileInputStream(file);
			//对象流
			ObjectInputStream oos = new ObjectInputStream(in);
			//写入输出Student对象
			Student[] students = (Student[])oos.readObject();
			oos.close();
			System.out.println(Arrays.toString(students));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

}

transient关键字:

如果用transient声明一个实例变量,当对象存储时,它的值不需要维持

在序列化时,id自动被忽略,反序列化时,int默认为0 

猜你喜欢

转载自blog.csdn.net/LemonSnm/article/details/89843650