Java IO learning notes (5) object stream and io learning notes

Source: Internet
Author: User

Java IO learning notes (5) object stream and io learning notes
1. Object stream: directly write or read the Object. 2. serialization: directly convert an Object into a word and write it to a hard disk or network. 3. If you want to convert an object into word throttling, the object class of this object must implement the Serializable interface. The Serializable interface is a mark interface and has no method, it is only used to identify whether the class can be serialized. 4. The transient keyword is used to modify the member variable, indicating that the member variable is transparent and used with the Serializable interface, indicating that the member variable is not considered during serialization. The default value of the Data Type of the member variable is stored during serialization. 5. The Externalizable interface is a subclass of the Serializable interface. The implementation of the Serializable interface JDK will help control the serialization process, and the implementation of the Externalizable interface can control the serialization implementation process. This interface has two methods: readExternal (ObjectInput in); read the object through ObjectInput. WriteExternal (ObjectOutput out); write the object through ObjectOutput. In general, it is better to implement JDK. This interface is not commonly used. Example:PackageTest. io. object;ImportJava. io. FileInputStream;ImportJava. io. FileOutputStream;ImportJava. io. IOException;ImportJava. io. ObjectInputStream;ImportJava. io. ObjectOutputStream;ImportJava. io. Serializable;Public ClassObjectStreamTest {Public Static VoidMain (String args []) {FileOutputStream fos =Null; ObjectOutputStream oos =Null; FileInputStream FCM =Null; ObjectInputStream ois =Null;Try{Fos =NewFileOutputStream ("E:/technology learning/java/test/Socket/objectTest.txt"); oos =NewObjectOutputStream (fos); Stu s =NewStu (); s. school = "dd"; oos. writeObject (s); FD =NewFileInputStream ("E:/technology learning/java/test/Socket/objectTest.txt"); ois =NewObjectInputStream (FS); Stu s2 = (Stu) ois. readObject (); System.Out. Print (s2.old + "" + s2.school + "" + s2.name + "" + s2.teacher );}Catch(ClassNotFoundException e) {e. printStackTrace ();}Catch(IOException e) {e. printStackTrace ();}}}ClassStuImplementsSerializable {Private Static Final Long SerialVersionUID= 1L;IntOld = 25; String name = "bjh ";TransientString teacher = "cj"; String school = "lgd";} running result: 25 dd bjh null

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.