创建实体类,实现序列化接口类:
package com.kgc7;
import java.io.Serializable;
//定义一个Student类,实现接口(implements Serializable)
public class Student implements Serializable{
String name;
String gender;
int age;
public Student(String name, String gender, int age) {
this.name = name;
this.gender = gender;
this.age = age;
}
}
单个实体序列化测试:
package com.kgc7;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
//序列化信息到默认保存文件"stu.txt",需要使用文件输出流FileOutputStream,对象输出流ObjectOutputStream。
public class Test {
public static void main(String[] args) {
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
fos = new FileOutputStream("stu.txt");
oos = new ObjectOu