序列化
来源:互联网 发布:力帆租车软件叫什么 编辑:程序博客网 时间:2024/06/05 00:10
序列化 (Serialization)将对象的状态信息转换为可以存储或传输的形式的过程,可以通过从存储区中读取或反序列化对象的状态,重新创建该对象。JAVA中对于对象的状态序列化和反序列化,提供了一对操作API,ObjectInputStream 和ObjectOutputStream 这两个可以将可序列化的对象的状态序列化到文件中保存或者传输。可序列化的对象,这个类必须是实现了Serializable接口的类,而且其成员变量也必须是可序列化的。
public class FilePathDemo implements Serializable{
}
序列化:
父类如果是可序列化的,子类也是可序列化的;
测试类:
控制台输出如下:
上述代码运行时会抛出异常,因为Test的一个成员变量Person没有继承序列化接口。如果让这个Person类继承Serializable接口,则不会抛出异常。
public class FilePathDemo implements Serializable{
}
序列化:
父类如果是可序列化的,子类也是可序列化的;
类的成员变量全部是可序列化的,类才能被正常可序列化。
下面举例将一组学生的信息存入文件中,再反序列化成对象输出来:
实体类:
package io.xlh;import java.io.Serializable;public class StudentInfo implements Serializable{private String name;private String stuNo;private String claName;public StudentInfo( String name,String stuNo, String claName) {this.name=name;this.stuNo=stuNo;this.claName=claName;}public String getStuNo() {return stuNo;}public void setStuNo(String stuNo) {this.stuNo = stuNo;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getClaName() {return claName;}public void setClaName(String claName) {this.claName = claName;}@Overridepublic String toString() {return name+","+stuNo+","+claName;}}
测试类:
package io.xlh;import java.io.*;import java.util.*;public class Serialization { @SuppressWarnings(value={"unchecked"}) public static void main(String[] args) throws Exception {String path =FileReader.class.getResource("/").getFile();String fileName="F:"+File.separator+"test.txt";List<StudentInfo> students =initStudents();serStudents(fileName, students);List<StudentInfo> stus=dserStudents(fileName);for(int i=0;i<stus.size();i++){System.out.println(stus.get(i));}} @SuppressWarnings(value={"unchecked"})public static List<StudentInfo> dserStudents(String fileName) {//反序列化 List<StudentInfo> list=new ArrayList<>(); try{ ObjectInputStream in=new ObjectInputStream(new FileInputStream(fileName)); list=(List<StudentInfo>) in.readObject(); in.close(); }catch (IOException e) {}catch (ClassNotFoundException e) {// TODO: handle exceptionlist=Collections.emptyList();}return list; }public static void serStudents(String fileName, List<StudentInfo> students) throws FileNotFoundException, IOException{ //序列化ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream(fileName)); out.writeObject(students); out.close();}public static List<StudentInfo> initStudents() {//初始化List<StudentInfo> students = new ArrayList<>();StudentInfo info=new StudentInfo("Lily", "100001", "java");students.add(info);info=new StudentInfo("Lucy", "100002", "C++");students.add(info);info=new StudentInfo("Lilei", "100003", "java");students.add(info);return students;}}
打开相应文件,可以看到里面一串二进制数字:
控制台输出如下:
Lily,100001,java
Lucy,100002,C++
Lilei,100003,java
证明反序列化成功。
例:
package io.pingtai;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectOutputStream;import java.io.Serializable;class Person {}public class Test implements Serializable { private Person p = new Person(); public static void main(String[] args) { Test t = new Test(); try { FileOutputStream fos = new FileOutputStream("F:"+File.separator+"data1.txt"); ObjectOutputStream os = new ObjectOutputStream(fos); os.writeObject(t); os.close(); } catch (IOException e) { e.printStackTrace(); } }}
上述代码运行时会抛出异常,因为Test的一个成员变量Person没有继承序列化接口。如果让这个Person类继承Serializable接口,则不会抛出异常。