Serialization and deserialization of Java

Source: Internet
Author: User

1  PackageCom.sun.demo;2 3 ImportJava.io.ByteArrayOutputStream;4 Importjava.io.IOException;5 ImportJava.io.ObjectInputStream;6 ImportJava.io.ObjectOutputStream;7 Importjava.io.Serializable;8 ImportJava.sql.Blob;9 Ten Importorg.hibernate.Hibernate; One  A  Public classSerializabledemoImplementsSerializable { -  -     Private Static Final LongSerialversionuid = 1L; the  -     PrivateBlob statistic; -  -     /** + * Convert object to BLOB type for saving to database -      *  +      * @paramobj A      * @return at      */ -      Publicjava.sql.Blob object2blob (Object obj) { -         Try { -Bytearrayoutputstream out =NewBytearrayoutputstream (); -ObjectOutputStream OutputStream =NewObjectOutputStream (out); - outputstream.writeobject (obj); in             byte[] bytes =Out.tobytearray (); - outputstream.close (); to             returnHibernate.createblob (bytes); +}Catch(Exception e) { - e.printstacktrace (); the         } *         return NULL; $     }Panax Notoginseng  -     /** the * Convert BLOBs to Objects +      *  A      * @paramDecblob the      * @return +      */ -      PublicObject blob2object (Java.sql.Blob decblob) { $         Try { $ObjectInputStream in =NewObjectInputStream (Decblob.getbinarystream ()); -Object obj =In.readobject (); - in.close (); the             returnobj; -}Catch(IOException e) {Wuyi e.printstacktrace (); the}Catch(Exception e) { - e.printstacktrace (); Wu         } -         return NULL; About     } $  -      PublicBlob getstatistic () { -         returnstatistic; -     } A  +      Public voidsetstatistic (Blob statistic) { the          This. Statistic =statistic; -     } $  the}

Here's a few notes.

One

In eclipse, there are two ways for us to quickly add serialversionuid.

Add default serial version ID:

Add generated serial version ID:

For the first, you need to know what is compatible and which are incompatible.

The second approach is to generate a hash value based on the structure of the class. Adding or subtracting a property, method, etc. can cause this value to change.

Serialization and deserialization of Java

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.