Java I/O operations Summary (4)

Source: Internet
Author: User
ArticleDirectory
    • Serializable serialization
    • Instance 2: deserialization of Objects
    • Pipeline Flow
    • Dataoutputstream and datainputstream

The main operations of Java Io have been completed.

This section describes other content about Java Io.

 

Serializable serialization instance 1: Object serialization
Import Java. io. file; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. io. objectoutputstream; import Java. io. serializable; @ suppresswarnings ("serial") // a class must implement the serializable interface class person implements serializable {private string name; private int age; Public Person (string name, int age) {This. name = Name; this. age = age;} Public String tostring () {return "name:" + this. name + ", age:" + this. age ;}} public class demo {public static void main (string [] ARGs) {string Path = file. separator + "home" + file. separator + "Siu" + file. separator + "work" + file. separator + "demo.txt"; person p1 = new person ("zhangsan", 12); person P2 = new person ("Lisi", 14 ); // here, the reference for creating a file writing stream is to play fileoutputstream Fos = NULL for the objectoutputstream constructor; objectoutputstream OOS = NULL; try {Fos = new fileoutputstream (PATH ); oos = new objectoutputstream (FOS); // you can write objects or other types of data to OOS. writeobject (P1); OOS. writeobject (P2);} catch (ioexception e) {e. printstacktrace ();} finally {try {OOS. close ();} catch (ioexception e) {e. printstacktrace ();}}}}

Object serialization is to store an object for persistence, so that its attributes can be easily retained.

In layman's terms, an object is pulled out of the heap memory and put on the hard disk.

Of course, if you are happy, you can serialize other things, including arrays and basic data types.

Let's take a look at the content ......

 

Instance 2: deserialization of Objects
Import Java. io. file; import Java. io. fileinputstream; import Java. io. ioexception; import Java. io. objectinputstream; public class demo {public static void main (string [] ARGs) {string Path = file. separator + "home" + file. separator + "Siu" + file. separator + "work" + file. separator + "demo.txt"; // Well, hereCodeIt takes a long time to write, and you need to throw exceptions or something. // if you are tired of reading it, you can leave it on the main method, you can use an anonymous object in the constructor. // What? Do not tell me that you do not know that the anonymous object fileinputstream is null; objectinputstream OIS = NULL; try {FD = new fileinputstream (PATH); ois = new objectinputstream (FCM ); // here the returned object is actually an object class Object // as we know it is a person Class Object //, It is locally transformed to person P = (person) Ois. readobject (); system. out. println (p); // get rid of you, annoying ~!!!} Catch (ioexception e) {e. printstacktrace ();} catch (classnotfoundexception e) {e. printstacktrace ();} finally {try {// remember to disable the downstream Ois. close ();} catch (ioexception e) {e. printstacktrace ();}}}}

You see, we store an object on the hard disk for future use.

Now you need it. Naturally, you have to take it out.

 

Pipeline stream instance 3: thread Communication
Import Java. io. ioexception; import Java. io. pipedinputstream; import Java. io. pipedoutputstream; // implement the runnable interface to implement a read Thread class read implements runnable {private pipedinputstream in; // spread the pipeline to be read to the constructor public read (pipedinputstream in) {This. in = In;} // read this thread public void run () {try {byte [] Buf = new byte [1024]; int temp = 0; // loop read // read is a blocking method and an exception needs to be thrown. // the code of the printed stream is added here // if no data is read, the printed code is also invalid while (temp = in. rea D (BUF ))! =-1) {string STR = new string (BUF, 0, temp); system. out. println (STR) ;}} catch (ioexception e) {// actually, a custom exception should be thrown here // I haven't figured out e yet. printstacktrace ();} finally {try {// I have thrown off the fire, just to remind myself that exceptions are very important in. close ();} catch (ioexception e) {e. printstacktrace () ;}}}// here implements a write class write implements runnable {private pipedoutputstream out; // transmits the pipe input to public write (pipedoutputstream out) {This. out = out;} public void run () {try {// write data out. write ("pipeline output ". getbytes ();} catch (ioexception e) {e. printstacktrace ();} finally {try {// actually, this close method should be written in the above try. // However, this is strange, and the logic is not very good. close ();} catch (ioexception e) {e. printstacktrace () ;}}} public class demo {public static void main (string [] ARGs) {pipedinputstream in = new pipedinputstream (); pipedoutputstream out = new pipedoutputstream (); try {// connect the MPs queue in. connect (out); // create an object and enable the thread. // put try again here... in catch, // if no pipeline is connected, the following operation does not make sense: Read r = new read (in); write W = new write (out ); // put the object that has implemented the run method into the thread and execute new thread (R ). start (); New thread (W ). start ();} catch (ioexception e) {e. printstacktrace ();}}}

Okay, I printed such a sentence, and it was annoying to discard exceptions. In order to pay attention to details ......

Pipeline stream may be hard to understand.

We know that byte streams and byte streams both require Arrays for stream transfer.

While pipeline streams directly concatenate two streams, sending data while receiving

However, how can we determine the consistency between sending and receiving in the two statuses of simultaneous communication?

Then, the thread is used, whether the receiver or the sender executes

It will always cause a thread to be blocked and wait for data from the other side to be passed over.

In general, the purpose of MPs queue flow is to achieve thread communication.

In addition, the pipedreader and pipedwriter classes have the same operating principles and will not be described here.

 

Dataoutputstream and datainputstream instance 4: Write of the basic data type
Import Java. io. dataoutputstream; import Java. io. file; import Java. io. fileoutputstream; import Java. io. ioexception; public class demo {public static void main (string [] ARGs) {string Path = file. separator + "home" + file. separator + "Siu" + file. separator + "work" + file. separator + "demo.txt"; dataoutputstream d = NULL; try {// The Object D of the outputstream class needs to be input here = new dataoutputstream (New fileoutputstream (PATH )); // start writing the basic data type D. writeint (12); D. writeboolean (true); D. writedouble (12.2223); D. writechar (97); // refresh the stream d. flush ();} catch (ioexception e) {e. printstacktrace ();} finally {try {d. close ();} catch (ioexception e) {e. printstacktrace ();}}}}

Here, we cannot intuitively understand the content, because it uses byte streams instead of bytes streams.

We only need to know, hereProgramThe basic data type has been written to the hard disk.

 

Instance 5: read of the basic data type
Import Java. io. datainputstream; import Java. io. file; import Java. io. fileinputstream; import Java. io. ioexception; public class demo {public static void main (string [] ARGs) {string Path = file. separator + "home" + file. separator + "Siu" + file. separator + "work" + file. separator + "demo.txt"; datainputstream d = NULL; try {d = new datainputstream (New fileinputstream (PATH); // read the basic data type system in the storage order. out. println (D. readint (); system. out. println (D. readboolean (); system. out. println (D. readdouble (); system. out. println (D. readchar ();} catch (ioexception e) {e. printstacktrace ();} finally {try {d. close ();} catch (ioexception e) {e. printstacktrace ();}}}}

Note that the data must be read in the write order; otherwise, a data printing error may occur.

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.