当前位置:网站首页>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();
}
}
边栏推荐
- Ananagrams(UVA156)
- A solution to the problem of "couldn't open file /mnt/repodata/repomd.xml"
- SQL error caused by entity class: Oracle "ora-00904" error: possible case of invalid identifier
- Memorize unfamiliar words at SSM stage and update them from time to time
- Imile uses Zadig's multi cloud environment to deploy thousands of times a week to continuously deliver global business across clouds and regions
- MySQL updates JSON string in array form
- Share an example of a simple MapReduce method using a virtual machine
- Myrpc version 0
- 节点CODE相同会导致数据重复
- Internship: interface case implementation
猜你喜欢

Troubleshoot abnormal video playback problems in public network deployment based on Haikang ehomedemo tool

Threejs实现模拟河流,水面水流,水管水流,海面

The new paradigm of AI landing is "hidden" in the next major upgrade of software infrastructure

An error occurs when sqlyog imports the database. Please help solve it!
![Tea mall system based on SSM framework [project source code + database script + report]](/img/d9/0a46c0da9839a7186bd3a9ae55f0a5.png)
Tea mall system based on SSM framework [project source code + database script + report]

Matlab reads fig file and restores signal

Redis sentry, persistence, master-slave, hand tear LRU

什么是光耦电路,实际使用中应该注意些什么?

File system and directory operations
![Blue Bridge Cup: magic cube rotation [Vocational group]](/img/ba/aeae2744f3aaa1052b5af452f990e2.jpg)
Blue Bridge Cup: magic cube rotation [Vocational group]
随机推荐
iMile 利用 Zadig 多云环境周部署千次,跨云跨地域持续交付全球业务
Es2016 key summary
JS reflect
Equity interest [non DP]
Salary management system based on servlet+jsp+mysql [source code + database]
管道实现进程间通信之命名管道
输入输出及中断技术——微机第六章学习笔记
Modifier of JS regular expression
Basic knowledge of redis
FortiGate creates multiple corresponding DDNS dynamic domain names for multiple ADSL interfaces
OneNote software
idea灰屏问题
JS file block to Base64 text
SQL error caused by entity class: Oracle "ora-00904" error: possible case of invalid identifier
Stack implementation integrated Calculator - code implementation
Imile uses Zadig's multi cloud environment to deploy thousands of times a week to continuously deliver global business across clouds and regions
Five methods to clear floating and their advantages and disadvantages
Tea mall system based on SSM framework [project source code + database script + report]
How the FortiGate firewall rejects a port by using the local in policy policy
Unity when entering a string in the editor, escape the input of characters