当前位置:网站首页>Serializable and Deserialize
Serializable and Deserialize
2022-06-30 04:30:00 【A fat man】
Serialization mechanism : By using ObjectInputStream and ObjectOutputStream The mechanism by which classes save and read objects is called serialization mechanism
object (Object) serialize The process of converting an object into a sequence of bytes
Deserialization Is the process of restoring objects according to byte sequence
Keep objects permanently , Save the byte sequence of the object to the local file
Passing objects through the network by serializing them
Passing objects between processes through serialization
An object wants to be serialized , The corresponding class must implement the serialization interface (Serializable)
Entity class :Student
package bean;
import java.io.Serializable;
/**
* @author : htf
*
* Entity class student object
*/
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private Integer Sid;
private String sName;
private transient String address;//transient Decorated properties cannot be serialized
public Student() {
}
public Student(Integer sid, String sName, String address) {
super();
Sid = sid;
this.sName = sName;
this.address = address;
}
public Integer getSid() {
return Sid;
}
public void setSid(Integer sid) {
Sid = sid;
}
public String getsName() {
return sName;
}
public void setsName(String sName) {
this.sName = sName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student [Sid=" + Sid + ", sName=" + sName + ", address=" + address + "]";
}
}
Test class :TestObjectSerial
package io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import bean.Student;
/**
* @author : htf
* Simulate persisting objects to hard disk files
* Serialization of objects requires : Implementation interface Serializable
*/
public class TestObjectSerial {
//Object--->String
public static void test1() {
// Instantiate objects
Student s1=new Student(1, "zhangsan", " Guangzhou ");
File parent=new File("E:\\temp");
// Read whether there is on the document
if(!parent.exists()) {
parent.mkdir();// create a file
}
File file =new File(parent,"student.txt");
// Manage resources automatically
try(ObjectOutputStream os=new ObjectOutputStream(new FileOutputStream(file));){
os.writeObject(s1);// stay e disc ,temp Folder ,student.txt View yes The result of serializing an object into bytes
}catch (Exception e) {
e.printStackTrace();
}
}
//String--->Object
public static void test2() {
File parent=new File("E:\\temp");
if(!parent.exists()) {
parent.mkdir();
}
File file =new File(parent,"student.txt");
try(ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));){
Student obj=(Student)ois.readObject();
System.out.println(obj);//transient Decorated properties cannot be serialized
// Deserialize characters into objects Student [Sid=1, sName=zhangsan, address=null]
}catch (Exception e) {
// TODO: handle exception
}
}
public static void main(String[] args) {
//test1();
test2();
}
}
边栏推荐
- Error Nova missingauthplugin: an auth plugin is required to determine endpoint URL
- FortiGate firewall and Aruze cloud tunnel interruption
- Idea grey screen problem
- Qt 6.3.1Conan软件包发布
- Interprocess communication
- Machine learning notes
- AI落地的新范式,就“藏”在下一场软件基础设施的重大升级里
- What is the difference between synchronized and lock
- How to write a conditional statement to obtain the value of the maximum time in a table using a MySQL statement
- OneNote production schedule
猜你喜欢
随机推荐
Blocking queue example
Qt 6.3.1Conan软件包发布
2021-07-14
oslo_ config. cfg. ConfigFileParseError: Failed to parse /etc/glance/glance-api. Conf: a solution to errors
Pig-Latin (UVA492)
Blue Bridge Cup: magic cube rotation [Vocational group]
Error Nova missingauthplugin: an auth plugin is required to determine endpoint URL
Named pipes for interprocess communication
JS inheritance
[从零开始学习FPGA编程-52]:高阶篇 - 基于IP核的FPGA开发 - IP核使用的基本框架(以锁相环PLL为例)
SQL error caused by entity class: Oracle "ora-00904" error: possible case of invalid identifier
I spent three years in a big factory outsourcing, which subverted my understanding!
OneNote production schedule
MySQL updates JSON string in array form
2021-11-04
深度融合云平台,对象存储界的“学霸”ObjectScale来了
Myrpc version 3
Equity interest [non DP]
Detailed explanation of data link layer
QT 6.3.1conan software package release








