当前位置:网站首页>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();
}
}
边栏推荐
- JS file block to Base64 text
- Myrpc version 3
- Interview topic of MySQL
- Blocking queue example
- Threejs实现模拟河流,水面水流,水管水流,海面
- 487-3279(POJ1002)
- Anonymous pipeline for interprocess communication
- MySQL updates JSON string in array form
- Thinkphp5 implements import function
- Troubleshooting of abnormal communication between FortiGate and fortiguard cloud
猜你喜欢
随机推荐
Qt Creator 8 Beta2发布
oslo_ config. cfg. ConfigFileParseError: Failed to parse /etc/glance/glance-api. Conf: a solution to errors
JS reflect
进程间通信之匿名管道
internship:接口案例实现
Differences between beanfactory and factorybean
Matlab reads fig file and restores signal
Threejs realizes the simulation of river, surface flow, pipe flow and sea surface
Equity interest [non DP]
MySQL updates JSON string in array form
Day 12 advanced programming techniques
Basic knowledge of redis
Learning about signals
Learn about threads
thinkphp5实现导入功能
7-3 打怪升级 单源最短路
base64.c
JS deconstruction assignment
Sql语句遇到的错误,求解
输入输出及中断技术——微机第六章学习笔记









