OBJECTT stream of I/O flows in Java

Source: Internet
Author: User

Object flow in Java: Writing or Reading object objects directly

1. Serializable interface: serialization, can be serialized, if you really need to write a class of objects on the hard disk or on the network, want to serialize them into a byte stream, you must implement this interface (a labeled interface, used to mark the object of this class can be serialized, So how exactly this class is serialized and cannot be controlled is the JDK's serialization process that directly helps you control the object of the class.

  2. Transient keyword: Used to modify member variables

3. Externalizable interface: Through the readexternal (), writeexternal () This two methods, the implementation of the serialization process.

Demo_1:

Import Java.io.*;class TT implements Serializable {//Although no method is overridden, the compiler sees this tag and knows that the class can be serialized int i = 10;int J = 9;double d = 2.3;int k1 = 15;transient int k2 = 15; The equivalent of this K2 is transparent, in the serialization of the time not to consider, but also just write to the hard disk, do not write K2 value, read out is the default value 0}class Test {public static void main (string[] args) {TT TT = new TT (); tt.k1 = 8;try {FileOutputStream fos = new FileOutputStream ("E:/eclipse projiect/test0505.txt"); ObjectOutputStream Oos = new ObjectOutputStream (FOS); Oos.writeobject (TT); Oos.flush (); Oos.close (); FileInputStream fis = new FileInputStream ("E:/eclipse projiect/test0505.txt"); ObjectInputStream ois = new ObjectInputStream (FIS); TT ttread = (TT) ois.readobject (); The object type is read, and it needs to be cast to the TT type SYSTEM.OUT.PRINTLN (ttread.i+ "" +ttread.j+ "" +ttread.d+ "" +ttread.k1+ "+ttread.k2);} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} catch ( ClassNotFoundException e) {e.printstacktrace ();}}}

Operating Result: 10 9 2.3 8 0

The content in the TEST0505.txt file is: SR TT (Min crunch?) D DI II JI [email protected]ffffff

OBJECTT stream of I/O flows in 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.