Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to write Java native serialization and deserialization code

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, the editor will bring you about how to write the native serialization and deserialization code of Java. The article is rich in content and analyzes and describes it from a professional point of view. I hope you can get something after reading this article.

Write a Java native serialization and deserialization DEMO.

Classes to be serialized:

Package com.nicchagil.nativeserialize;import java.io.Serializable;public class User implements Serializable {private static final long serialVersionUID = 1L; private Integer id; private String userName; public User (Integer id, String userName) {super (); this.id = id; this.userName = userName;} public Integer getId () {return id;} public void setId (Integer id) {this.id = id;} public String getUserName () {return userName;} public void setUserName (String userName) {this.userName = userName } public static long getSerialversionuid () {return serialVersionUID;} @ Override public int hashCode () {final int prime = 31; int result = 1; result = prime * result + ((id = = null)? 0: id.hashCode ()); result = prime * result + ((userName = = null)? 0: userName.hashCode ()); return result;} @ Override public boolean equals (Object obj) {if (this = obj) return true; if (obj = = null) return false If (getClass ()! = obj.getClass () return false; User other = (User) obj; if (id = = null) {if (other.id! = null) return false;} else if (! id.equals (other.id)) return false; if (userName = = null) {if (other.userName! = null) return false;} else if (! userName.equals (other.userName)) return false; return true @ Override public String toString () {return "User [id=" + id + ", userName=" + userName + "]";}}

Tool class:

Package com.nicchagil.nativeserialize;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.io.Serializable Public class NativeSerializeTools {/ * serialization * @ param filePath serialized path * @ param s serialized object * / public static void write (String filePath, Serializable s) throws FileNotFoundException, IOException {if (filePath = = null | | filePath.length () = = 0) {throw new RuntimeException ("Please pass in the serialization path");} if (s = = null) {throw new RuntimeException ("Please pass in the serialized object");} File f = new File (filePath); ObjectOutputStream oos = null FileOutputStream fos = null; try {fos = new FileOutputStream (f); oos = new ObjectOutputStream (fos); oos.writeObject (s); System.out.println ("finish.");} finally {if (oos! = null) {oos.close ();} if (fos! = null) {fos.close ();} System.out.println ("close the resource.") }} / * deserialization * @ param filePath deserialization path * @ return deserialization object * / public static Object read (String filePath) throws ClassNotFoundException, FileNotFoundException, IOException {if (filePath = = null | | filePath.length () = = 0) {throw new RuntimeException ("Please pass in the deserialization path");} File f = new File (filePath); ObjectInputStream ois = null; FileInputStream fis = null; Object o = null; try {fis = new FileInputStream (f) Ois = new ObjectInputStream (fis); o = ois.readObject (); System.out.println ("finish.");} finally {if (ois! = null) {ois.close ();} if (fis! = null) {fis.close ();} System.out.println ("close the resource.");} return o;}}

Test class:

Package com.nicchagil.nativeserialize;import java.io.FileNotFoundException;import java.io.IOException;import org.junit.Assert;import org.junit.Test;public class HowToUse {private User user = new User (100, "Nick Huang"); private String filePath = "d:/user.txt"; @ Test public void C1 () throws FileNotFoundException, IOException {NativeSerializeTools.write (filePath, user);} @ Test public void c2 () throws FileNotFoundException, IOException, ClassNotFoundException {Object o = NativeSerializeTools.read (filePath); System.out.println (o) Assert.assertTrue (user.equals (o));}}

Log:

Finish.close the resource.finish.close the resource.User [id=100, userName=Nick Huang]

This is how the Java native serialization and deserialization code shared by Xiaobian is written. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report