Java-Object Serialization Code instance

Source: Internet
Author: User

1 ImportJava.io.ByteArrayInputStream;2 ImportJava.io.ByteArrayOutputStream;3 ImportJava.io.ObjectInputStream;4 ImportJava.io.ObjectOutputStream;5 Importjava.io.Serializable;6 7  Public classUserImplementsSerializable {8     PrivateString username; 9     Ten     Private transientString password;  One        A      PublicUser (String username,string password) { -          This. Username =username;  -            the          This. Password =password;  -     }   -    - @Override +      PublicString toString () { -         returnUsername + "" +password;  +     }   A        at      Public Static voidMain (string[] args)throwsexception{ -User User =NewUser ("Weidong", "123456");  -            -System.out.println ("before serialization:" +user);  -            -Bytearrayoutputstream buf =NewBytearrayoutputstream ();  in         //creates an object output stream that WriteObject () writes objects to the output stream through the object output stream -ObjectOutputStream out =NewObjectOutputStream (BUF);  to            + out.writeobject (user);  -            the Out.close ();  *         //creates an object input stream that reads objects from the input stream through the ReadObject () of the object input stream $ObjectInputStream in =NewObjectInputStream (NewBytearrayinputstream (Buf.tobytearray ())); Panax Notoginseng            -user =(User) in.readobject ();  the            +System.out.println ("After serialization:" +user);  A            the In.close ();  +     }   - } $  $ before serialization:weidong123456 -After Serialization:weidongnull

Java-Object Serialization Code instance

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.