目录
一、序列化概念
二、实现序列化的方法
1.Serializable接口:
1.Serializable接口的作用
2.Serializable接口为空的原因
2.serialVersionUID
1.serialVersionUID的概念
2.serialVersionUID的生成
3.Serializable接口的使用
三、序列化对象的输入与输出
1.序列化对象的输出
2.序列化对象的输入
3.主函数
四、自定义序列化的方法
五、通过序列化实现学生管理系统的增删改查操作
学生类:
主类:
添加类:
删除类:
修改类:
查询类:
展示类:
序列化是指把内存中的Java对象转化为与平台无关的二进制字节序列,以便永久保存在磁盘上或通过网络进行传输,是Java提供的一种将对象写入到输出流并在之后将其读回的机制。
序列化:把Java对象转化为字节序列
反序列化:把字节序列恢复为Java对象
让某个对象支持序列化机制>让对象所属的类及其属性可序列化>让该类实现Serializable接口
public interface Serializable {
}
public interface Serializable类通过实现 java.io.Serializable 接口以启用其序列化功能。未实现此接口的类将无法使其任何状态序列化或反序列化。
Serializable只起到了一个标识的作用,告诉程序实现了它的对象是可以被序列化的。ObjectOutputStream才是实现序列化的关键类。
serialVersionUID是表示序列化版本标识符的静态常量。凡是实现Serializable接口的类都有一个serialVersionUID来表明类的不同版本间的兼容性。
serialVersionUID可由自己定义,其中数字后面加上的L表示这是一个long值。如果其值未被定义,它将根据Java运行时的内部细节自动化生成。
import java.io.Serializable;
public class Person implements Serializable {
public static final long serialVersionUID = 475463534532L;
private String name;
private int age;
private int id;
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return this.id;
}
public Person(String name, int age, int id) {
this.name = name;
this.age = age;
this.id = id;
}
public String toString() {
return name + " " + age + " " + id;
}
}
通过ObjectOutputStream类对象的writeObject方法,将指定的对象序列化,把得到的字节序列写到目标输出流中。
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class ObjectOutputStreamTest {
public static void objectOutputStreamTest(ObjectOutputStream oos, FileOutputStream fos, String path) {
try {
fos = new FileOutputStream(path);
oos = new ObjectOutputStream(fos);
oos.writeObject(new Person("康茹", 21, 123));
oos.flush();
oos.writeObject(new Person("小白", 20, 124));
oos.flush();
} catch (IOException e) {
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
}
}
}
}
}
通过ObjectInputStream类对象的readObject方法从源输入流中读取字节序列,再反序列化为对象,并将其返回。
import java.io.EOFException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class ObjectInputStreamTest {
public static void objectInputStreamTest(ObjectInputStream ois, String path) throws ClassNotFoundException {
try {
ois = new ObjectInputStream(new FileInputStream(path));
while (true) {
Person p = (Person) ois.readObject();
System.out.println(p);
}
} catch (EOFException e) {
} catch (IOException e) {
} catch (ClassCastException e) {
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
}
}
}
}
}
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Main {
public static void main(String[] args) throws Exception {
ObjectInputStream ois = null;
ObjectOutputStream oos = null;
FileOutputStream fos = null;
String path = "object.dat";
ObjectOutputStreamTest.objectOutputStreamTest(oos, fos, path);
ObjectInputStreamTest.objectInputStreamTest(ois, path);
}
}
1.使用使用transient关键字。对需要进行序列化的类进行定义时,使用transient关键字声明无需序列化的字段。默认序列化机制会忽略被transient修饰的字段。
2.使用Externalizable接口。使用该接口之后,之前基于Serializable接口的序列化机制就将失效,必须重写readExternal()和writeExternal()方法。
import java.io.Serializable;
public class Student implements Serializable {
private int id;
private String name;
private double score;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public Student(int id, String name, double score) {
this.id = id;
this.name = name;
this.score = score;
}
@Override
public String toString() {
return id + "\t" + name + "\t" + score;
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
while (true) {
System.out.println("1.增加 2.删除 3.修改 4.查询 5.展示 6.退出");
System.out.println("请选择操作:");
String num = in.next();
if (num.equals("1")) {
Add.add();
} else if (num.equals("2")) {
Delete.delete();
} else if (num.equals("3")) {
Revise.revise();
} else if (num.equals("4")) {
Seek.seek();
} else if (num.equals("5")) {
Show.show();
} else if (num.equals("6")) {
System.out.println("退出系统");
break;
} else {
System.out.println("没有此操作,请重新输入!");
}
}
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;
public class Add {
public static void add() {
Scanner in = new Scanner(System.in);
ArrayList students = new ArrayList<>();
try {
FileInputStream fis = new FileInputStream("Student.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
while (true) {
if ((ois.available()) != -1) {
Student student = (Student) ois.readObject();
students.add(student);
} else {
break;
}
}
} catch (Exception e) {
}
try {
System.out.println("请输入要添加的学生的学号:");
int id = in.nextInt();
if (id != 0) {
System.out.println("请输入学生的姓名:");
String name = in.next();
System.out.println("请输入学生的成绩:");
double score = in.nextDouble();
Student student = new Student(id, name, score);
students.add(student);
System.out.println("添加成功!");
}
FileOutputStream fis = new FileOutputStream("Student.txt");
ObjectOutputStream oos = new ObjectOutputStream(fis);
for (int i = 0; i < students.size(); i++) {
oos.writeObject(students.get(i));
}
} catch (Exception e) {
}
System.out.println();
return;
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;
public class Delete {
public static void delete() {
Scanner sc = new Scanner(System.in);
ArrayList students = new ArrayList<>();
students = new ArrayList();
try {
FileInputStream fis = new FileInputStream("Student.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
while (true) {
if ((ois.available()) != -1) {
Student student = (Student) ois.readObject();
students.add(student);
} else {
break;
}
}
} catch (Exception e) {
}
try {
System.out.println("请输入要删除的学生学号:");
int id = sc.nextInt();
boolean flag = false;
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId() == id) {
students.remove(i);
System.out.println("删除成功!");
flag = true;
break;
}
}
if (flag == false) {
System.out.println("输入的学号不存在!");
}
FileOutputStream fos = new FileOutputStream("Student.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
for (int i = 0; i < students.size(); i++) {
oos.writeObject(students.get(i));
}
} catch (Exception e) {
}
System.out.println();
return;
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Scanner;
public class Revise {
public static void revise() {
Scanner in = new Scanner(System.in);
ArrayList students = new ArrayList<>();
try {
FileInputStream fis = new FileInputStream("Student.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
while (true) {
if ((ois.available()) != -1) {
Student student = (Student) ois.readObject();
students.add(student);
} else {
break;
}
}
} catch (Exception e) {
}
try {
System.out.println("请输入要修改的学生学号:");
int id = in.nextInt();
Boolean flag = false;
for (int i = 0; i < students.size(); i++) {
if (students.get(i).getId() == id) {
System.out.println("请输入学生的姓名:");
String name = in.next();
System.out.println("请输入学生的成绩:");
double score = in.nextDouble();
Student student = new Student(id, name, score);
students.set(i, student);
flag = true;
break;
}
}
if (flag == false) {
System.out.println("输入的学号不存在");
}
FileOutputStream fos = new FileOutputStream("Student.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
for (int i = 0; i < students.size(); i++) {
oos.writeObject(students.get(i));
}
} catch (Exception e) {
}
System.out.println();
return;
}
}
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Scanner;
public class Seek {
public static void seek() {
Scanner in = new Scanner(System.in);
ArrayList students = new ArrayList<>();
try {
FileInputStream fis = new FileInputStream("Student.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
while (true) {
if ((ois.available()) != -1) {
Student student = (Student) ois.readObject();
students.add(student);
} else {
break;
}
}
} catch (Exception e) {
}
try {
System.out.println("请输入要查询的学生学号:");
int id = in.nextInt();
FileInputStream fis = new FileInputStream("Student.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
while (true) {
if ((ois.available()) != -1) {
Student student = (Student) ois.readObject();
if (student.getId() == id) {
System.out.println("学号\t姓名\t成绩");
System.out.println(student);
}
} else {
break;
}
}
} catch (Exception e) {
}
System.out.println();
return;
}
}
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.util.ArrayList;
public class Show {
public static void show() {
ArrayList students = new ArrayList<>();
try {
FileInputStream fis = new FileInputStream("Student.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
while (true) {
if ((ois.available()) != -1) {
Student student = (Student) ois.readObject();
students.add(student);
} else {
break;
}
}
} catch (Exception e) {
}
try {
System.out.println("学号\t姓名\t成绩");
FileInputStream fis = new FileInputStream("Student.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
while (true) {
if ((ois.available()) != -1) {
Student student = (Student) ois.readObject();
System.out.println(student);
} else {
break;
}
}
} catch (Exception e) {
}
System.out.println();
return;
}
}