一. 序列化和反序列化的概念
序列化:将对象转换成字节序列的过程称为对象的序列化。
反序列化:将字节序列恢复成对象的过程称为对象的反序列化。
二. 什么时候需要序列化
对象序列化的场景
1) 需要将内存中对象的字节序列永久化保存到磁盘中,通常是放在一个文件中;
2) 在网络上传送对象的字节序列到任何其他程序。
三. 如何进行序列化和反序列化
序列化的步骤:
1)创建一个对象输出流ObjectOutputStream ,它可以包装一个目标对象的输出流,比如文件输出流;
2)使用对象输出流的writeObject()方法写对象。
反序列化的步骤:
1)创建一个对象输入流ObjectInputStream,它可以包装成一个目标对象的输入流,比如文件输入流;
2)使用对象输入流的readObject()方法读对象。
四. 序列化在项目中的应用
1 import java.io.Serializable; 2 3 public class Student implements Serializable { 4 5 private String name; 6 private int age; 7 8 public String getName() { 9 return name; 10 } 11 12 public void setName(String name) { 13 this.name = name; 14 } 15 16 public int getAge() { 17 return age; 18 } 19 20 public void setAge(int age) { 21 this.age = age; 22 } 23 }
1 import com.abc.bean.Student; 2 import org.junit.Test; 3 4 import java.io.*; 5 6 public class TestSerializeAndDeserizlize { 7 8 /** 9 * 序列化 10 */ 11 @Test 12 public void testSerialize() throws IOException { 13 Student student = new Student(); 14 student.setName("天使"); 15 student.setAge(1); 16 // 对象输出流 17 ObjectOutputStream oo = new ObjectOutputStream(new FileOutputStream(new File("E:/student.txt"))); 18 oo.writeObject(student); 19 System.out.println("Student对象序列化成功"); 20 } 21 22 @Test 23 public void testDeserrialize() throws IOException, ClassNotFoundException { 24 // 对象输入流 25 ObjectInputStream oi = new ObjectInputStream(new FileInputStream(new File("E:/student.txt"))); 26 Student student = (Student) oi.readObject(); 27 System.out.println(student.getAge()); 28 System.out.println(student.getName()); 29 System.out.println("student对象反序列化成功"); 30 31 } 32 }
# 控制台输出
"C:Program FilesJavajdk1.8.0_261injava.exe" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:50887,suspend=y,server=n -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:C:UsersAdministratorAppDataLocalJetBrainsIntelliJIdea2020.2captureAgentdebugger-agent.jar -Dfile.encoding=UTF-8 -classpath "D:IntelliJ IDEA 2020.2libidea_rt.jar;D:IntelliJ IDEA 2020.2pluginsjunitlibjunit5-rt.jar;D:IntelliJ IDEA 2020.2pluginsjunitlibjunit-rt.jar;C:Program FilesJavajdk1.8.0_261jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_261jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_261jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_261jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_261jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_261jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_261jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_261jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_261jrelibext ashorn.jar;C:Program FilesJavajdk1.8.0_261jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_261jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_261jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_261jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_261jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_261jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_261jrelibjce.jar;C:Program FilesJavajdk1.8.0_261jrelibjfr.jar;C:Program FilesJavajdk1.8.0_261jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_261jrelibjsse.jar;C:Program FilesJavajdk1.8.0_261jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_261jrelibplugin.jar;C:Program FilesJavajdk1.8.0_261jrelib esources.jar;C:Program FilesJavajdk1.8.0_261jrelib t.jar;E:parttime 1-primary arget est-classes;E:parttime 1-primary argetclasses;D:apache-maven-3.6.3 epositoryorgspringframeworkootspring-boot-test-autoconfigure2.4.0spring-boot-test-autoconfigure-2.4.0.jar;D:apache-maven-3.6.3 epositoryorgspringframeworkootspring-boot-starter-web2.4.0spring-boot-starter-web-2.4.0.jar;D:apache-maven-3.6.3 epositoryorgspringframeworkootspring-boot-starter2.4.0spring-boot-starter-2.4.0.jar;D:apache-maven-3.6.3 epositoryorgspringframeworkootspring-boot2.4.0spring-boot-2.4.0.jar;D:apache-maven-3.6.3 epositoryorgspringframeworkootspring-boot-autoconfigure2.4.0spring-boot-autoconfigure-2.4.0.jar;D:apache-maven-3.6.3 epositoryorgspringframeworkootspring-boot-starter-logging2.4.0spring-boot-starter-logging-2.4.0.jar;D:apache-maven-3.6.3 epositorychqoslogbacklogback-classic1.2.3logback-classic-1.2.3.jar;D:apache-maven-3.6.3 epositorychqoslogbacklogback-core1.2.3logback-core-1.2.3.jar;D:apache-maven-3.6.3 epositoryorgapachelogginglog4jlog4j-to-slf4j2.13.3log4j-to-slf4j-2.13.3.jar;D:apache-maven-3.6.3 epositoryorgapachelogginglog4jlog4j-api2.13.3log4j-api-2.13.3.jar;D:apache-maven-3.6.3 epositoryorgslf4jjul-to-slf4j1.7.30jul-to-slf4j-1.7.30.jar;D:apache-maven-3.6.3 epositoryjakartaannotationjakarta.annotation-api1.3.5jakarta.annotation-api-1.3.5.jar;D:apache-maven-3.6.3 epositoryorgyamlsnakeyaml1.27snakeyaml-1.27.jar;D:apache-maven-3.6.3 epositoryorgspringframeworkootspring-boot-starter-json2.4.0spring-boot-starter-json-2.4.0.jar;D:apache-maven-3.6.3 epositorycomfasterxmljacksoncorejackson-databind2.11.3jackson-databind-2.11.3.jar;D:apache-maven-3.6.3 epositorycomfasterxmljacksoncorejackson-annotations2.11.3jackson-annotations-2.11.3.jar;D:apache-maven-3.6.3 epositorycomfasterxmljacksoncorejackson-core2.11.3jackson-core-2.11.3.jar;D:apache-maven-3.6.3 epositorycomfasterxmljacksondatatypejackson-datatype-jdk82.11.3jackson-datatype-jdk8-2.11.3.jar;D:apache-maven-3.6.3 epositorycomfasterxmljacksondatatypejackson-datatype-jsr3102.11.3jackson-datatype-jsr310-2.11.3.jar;D:apache-maven-3.6.3 epositorycomfasterxmljacksonmodulejackson-module-parameter-names2.11.3jackson-module-parameter-names-2.11.3.jar;D:apache-maven-3.6.3 epositoryorgspringframeworkootspring-boot-starter-tomcat2.4.0spring-boot-starter-tomcat-2.4.0.jar;D:apache-maven-3.6.3 epositoryorgglassfishjakarta.el3.0.3jakarta.el-3.0.3.jar;D:apache-maven-3.6.3 epositoryorgapache omcatembed omcat-embed-websocket9.0.39 omcat-embed-websocket-9.0.39.jar;D:apache-maven-3.6.3 epositoryorgspringframeworkspring-web5.3.1spring-web-5.3.1.jar;D:apache-maven-3.6.3 epositoryorgspringframeworkspring-beans5.3.1spring-beans-5.3.1.jar;D:apache-maven-3.6.3 epositoryorgspringframeworkspring-webmvc5.3.1spring-webmvc-5.3.1.jar;D:apache-maven-3.6.3 epositoryorgspringframeworkspring-aop5.3.1spring-aop-5.3.1.jar;D:apache-maven-3.6.3 epositoryorgspringframeworkspring-context5.3.1spring-context-5.3.1.jar;D:apache-maven-3.6.3 epositoryorgspringframeworkspring-expression5.3.1spring-expression-5.3.1.jar;D:apache-maven-3.6.3 epositoryorgspringframeworkootspring-boot-starter-test2.4.0spring-boot-starter-test-2.4.0.jar;D:apache-maven-3.6.3 epositoryorgspringframeworkootspring-boot-test2.4.0spring-boot-test-2.4.0.jar;D:apache-maven-3.6.3 epositorycomjaywayjsonpathjson-path2.4.0json-path-2.4.0.jar;D:apache-maven-3.6.3 epository etminidevjson-smart2.3json-smart-2.3.jar;D:apache-maven-3.6.3 epository etminidevaccessors-smart1.2accessors-smart-1.2.jar;D:apache-maven-3.6.3 epositoryorgow2asmasm5.0.4asm-5.0.4.jar;D:apache-maven-3.6.3 epositoryorgslf4jslf4j-api1.7.30slf4j-api-1.7.30.jar;D:apache-maven-3.6.3 epositoryjakartaxmlindjakarta.xml.bind-api2.3.3jakarta.xml.bind-api-2.3.3.jar;D:apache-maven-3.6.3 epositoryjakartaactivationjakarta.activation-api1.2.2jakarta.activation-api-1.2.2.jar;D:apache-maven-3.6.3 epositoryorgassertjassertj-core3.18.1assertj-core-3.18.1.jar;D:apache-maven-3.6.3 epositoryorghamcresthamcrest2.2hamcrest-2.2.jar;D:apache-maven-3.6.3 epositoryorgjunitjupiterjunit-jupiter5.7.0junit-jupiter-5.7.0.jar;D:apache-maven-3.6.3 epositoryorgjunitjupiterjunit-jupiter-api5.7.0junit-jupiter-api-5.7.0.jar;D:apache-maven-3.6.3 epositoryorgapiguardianapiguardian-api1.1.0apiguardian-api-1.1.0.jar;D:apache-maven-3.6.3 epositoryorgopentest4jopentest4j1.2.0opentest4j-1.2.0.jar;D:apache-maven-3.6.3 epositoryorgjunitplatformjunit-platform-commons1.7.0junit-platform-commons-1.7.0.jar;D:apache-maven-3.6.3 epositoryorgjunitjupiterjunit-jupiter-params5.7.0junit-jupiter-params-5.7.0.jar;D:apache-maven-3.6.3 epositoryorgjunitjupiterjunit-jupiter-engine5.7.0junit-jupiter-engine-5.7.0.jar;D:apache-maven-3.6.3 epositoryorgjunitplatformjunit-platform-engine1.7.0junit-platform-engine-1.7.0.jar;D:apache-maven-3.6.3 epositoryorgmockitomockito-core3.6.0mockito-core-3.6.0.jar;D:apache-maven-3.6.3 epository etytebuddyyte-buddy1.10.18yte-buddy-1.10.18.jar;D:apache-maven-3.6.3 epository etytebuddyyte-buddy-agent1.10.18yte-buddy-agent-1.10.18.jar;D:apache-maven-3.6.3 epositoryorgobjenesisobjenesis3.1objenesis-3.1.jar;D:apache-maven-3.6.3 epositoryorgmockitomockito-junit-jupiter3.6.0mockito-junit-jupiter-3.6.0.jar;D:apache-maven-3.6.3 epositoryorgskyscreamerjsonassert1.5.0jsonassert-1.5.0.jar;D:apache-maven-3.6.3 epositorycomvaadinexternalgoogleandroid-json .0.20131108.vaadin1android-json-0.0.20131108.vaadin1.jar;D:apache-maven-3.6.3 epositoryorgspringframeworkspring-core5.3.1spring-core-5.3.1.jar;D:apache-maven-3.6.3 epositoryorgspringframeworkspring-jcl5.3.1spring-jcl-5.3.1.jar;D:apache-maven-3.6.3 epositoryorgspringframeworkspring-test5.3.1spring-test-5.3.1.jar;D:apache-maven-3.6.3 epositoryorgxmlunitxmlunit-core2.7.0xmlunit-core-2.7.0.jar;D:apache-maven-3.6.3 epositoryorgspringframeworkootspring-boot-starter-actuator2.4.0spring-boot-starter-actuator-2.4.0.jar;D:apache-maven-3.6.3 epositoryorgspringframeworkootspring-boot-actuator-autoconfigure2.4.0spring-boot-actuator-autoconfigure-2.4.0.jar;D:apache-maven-3.6.3 epositoryorgspringframeworkootspring-boot-actuator2.4.0spring-boot-actuator-2.4.0.jar;D:apache-maven-3.6.3 epositoryiomicrometermicrometer-core1.6.1micrometer-core-1.6.1.jar;D:apache-maven-3.6.3 epositoryorghdrhistogramHdrHistogram2.1.12HdrHistogram-2.1.12.jar;D:apache-maven-3.6.3 epositoryorglatencyutilsLatencyUtils2.0.3LatencyUtils-2.0.3.jar;D:apache-maven-3.6.3 epositoryorgapache omcatembed omcat-embed-jasper9.0.39 omcat-embed-jasper-9.0.39.jar;D:apache-maven-3.6.3 epositoryorgapache omcatembed omcat-embed-core9.0.39 omcat-embed-core-9.0.39.jar;D:apache-maven-3.6.3 epositoryorgapache omcat omcat-annotations-api9.0.39 omcat-annotations-api-9.0.39.jar;D:apache-maven-3.6.3 epositoryorgapache omcatembed omcat-embed-el9.0.39 omcat-embed-el-9.0.39.jar;D:apache-maven-3.6.3 epositoryorgeclipsejdtecj3.18.0ecj-3.18.0.jar;D:apache-maven-3.6.3 epositoryjunitjunit4.13.1junit-4.13.1.jar;D:apache-maven-3.6.3 epositoryorghamcresthamcrest-core2.2hamcrest-core-2.2.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.abc.serialize.TestSerializeAndDeserizlize Connected to the target VM, address: '127.0.0.1:50887', transport: 'socket' 1 天使 student对象反序列化成功 Student对象序列化成功 Disconnected from the target VM, address: '127.0.0.1:50887', transport: 'socket' Process finished with exit code 0
参考博客:
https://www.cnblogs.com/xdp-gacl/p/3777987.html