The Java serialization object is a string and deserializes the string into an object

Source: Internet
Author: User
Tags object object serialization

The serialization and deserialization of an object is essentially a matter of saving the state of the object, typically saved to a file, but it is more commonly used to save the object as a string into a database, and then deserialize the string into an object if it is necessary to read the object.

Classes that can be serialized must implement the Serializabe interface, and it is important to note that if a property does not need to be serialized, you can add the transient keyword.

The following is a class that can be serialized:

 PackageDemo1;Importjava.io.Serializable; Public classPersonImplementsserializable{    Private Static Final LongSerialversionuid = 1L; Private intID; PrivateString name; Private intAge ; Private transientString sex;  PublicPerson (intID, String name,intage,string Sex) {        Super();  This. ID =ID;  This. Name =name;  This. Age =Age ;  This. Sex =sex; }     Public intgetId () {returnID; }     Public voidSetId (intID) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     Public voidSetage (intAge ) {         This. Age =Age ; }         PublicString Getsex () {returnsex; }     Public voidsetsex (String sex) { This. Sex =sex; } @Override PublicString toString () {return  This. id+ "" + This. name+ "" + This. age+ "" + This. Sex; }}

The following are the classes that encapsulate serialization:

 PackageDemo1;ImportJava.io.ByteArrayInputStream;ImportJava.io.ByteArrayOutputStream;Importjava.io.IOException;ImportJava.io.ObjectInputStream;ImportJava.io.ObjectOutputStream; Public classSerializeutils { Public StaticString Serialize (Object obj)throwsIOException {bytearrayoutputstream Bytearrayoutputstream=NewBytearrayoutputstream ();        ObjectOutputStream ObjectOutputStream; ObjectOutputStream=NewObjectOutputStream (Bytearrayoutputstream);        Objectoutputstream.writeobject (obj); String String= Bytearrayoutputstream.tostring ("Iso-8859-1");        Objectoutputstream.close ();        Bytearrayoutputstream.close (); returnstring; }     Public StaticObject serializetoobject (String str)throwsIOException, classnotfoundexception {bytearrayinputstream bytearrayinputstream=NewBytearrayinputstream (Str.getbytes ("Iso-8859-1")); ObjectInputStream ObjectInputStream=NewObjectInputStream (Bytearrayinputstream); Object Object=Objectinputstream.readobject ();        Objectinputstream.close ();        Bytearrayinputstream.close (); returnobject; }}

The following is a process that uses Redis to simply serialize the object to the database and then remove the data from the database for deserialization:

 Public classdemoatomic { Public Static voidMain (String [] args)throwsIOException, classnotfoundexception {Jedis Jedis=NewJedis ("localhost", 6379); Jedis.setex ("Test1", 60*60*24,serializeutils.serialize (NewPerson (1, "Test 1", 21, "male"))); Jedis.setex ("Test2", 60*60*24,serializeutils.serialize (NewPerson (2, "Test 2", 21, "female"))); Jedis.setex ("Test3", 60*60*24,serializeutils.serialize (NewPerson (3, "Test 3", 21, "male"))); Jedis.setex ("Test4", 60*60*24,serializeutils.serialize (NewPerson (4, "Test 4", 21, "female"))); Set<String> keys = Jedis.keys ("*");  for(String s:keys) {System.out.println (s); Person Person=(person) serializeutils.serializetoobject (Jedis.get (s));        System.out.println (Person.tostring ()); }    }}

The following is the printed data:

test44  Test 4  nulltest22  Test 2  nulltest3 3  Test 3  nulltest11  Test 1  null

The Java serialization object is a string and deserializes the string into an object

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.