Objective
The previous article mainly describes the file character input stream filewriter, file character output stream filereader, file byte output stream fileoutputstream, file byte input stream FileInputStream, these are common flow classes. Of course, in addition to these stream classes, Java also provides a lot of flow classes for users, this article looks at other streams.
Pipe flow
pipeline flow is primarily used to connect two threads of communication . Pipeline flow is also divided into byte stream (PipedInputStream, PipedOutputStream) and character stream (Pipedreader, PipedWriter). For example, a pipedinputstream must be connected to a PipedOutputStream object to generate a communication pipeline, PipedOutputStream write data to the pipeline, PipedInputStream read data from the pipeline. The pipeline flow works as shown in the following:
Let's look at the usage of the pipe flow. Since the pipeline flow is used for inter-thread communication, there is bound to be a send thread and a receive thread, with two threads interacting with the data through the pipeline flow. First write a thread that sends the data:
PublicClass SenderImplements runnable{private pipedoutputstream out = new PipedOutputStream (); public PipedOutputStream Getoutputstream () {return out;} public void run () { String str = "Receiver, Hello!" try {Out.write (str.getbytes ()); // write data to the pipe stream (send) Out.close ();} catch (IOException e) {e.printstacktrace ();}}}
when using the stream to write the data, pay attention to whether the stream supports direct write String, not the string of the GetBytes () method to get the byte of the strings. Now that there is a thread to send the data, here's a thread to receive the data:
PublicClass ReceiverImplementsrunnable{Private PipedInputStream in =NewPipedInputStream ();PublicPipedInputStream getInputStream () {ReturnIn }public void run () { String s = null; byte b0[] = new byte[ 1024try {int length = In.read (B0); if ( -1!= length) {s = new String (B0, 0, length); System.out.println ("received the following message:" + s);} in.close (); } catch (IOException e) {e.printstacktrace ();}}}
Two threads have, write a main thread, use the Connect method of the pipeline output stream to connect the pipeline output stream and the pipeline input:
PublicStaticvoidMain (string[] args) {Try{Sender Sender =New Sender (); Receiver receiver = new receiver (); Thread senderthread = new thread (sender); Thread receiverthread = new thread (receiver); PipedOutputStream out = Sender.getoutputstream (); // write PipedInputStream in = Receiver.getinputstream (); Span style= "color: #008000;" >// read out out.connect (in); // send output to input catch (IOException e) {e.printstacktrace ();}}
The output should be obvious, as we all know that the receiving thread received the data sent from the sending thread through the pipeline flow output stream:
The following message was received: Receiver, Hello!
Note that PipedInputStream uses a 1024-byte fixed-size loop buffer, and the data written to PipedOutputStream is actually saved to the corresponding PipedInputStream internal buffer. When PipedInputStream performs a read operation, the data read actually comes from the internal buffer. If the corresponding PipedInputStream input buffer is full, any thread attempting to write to PipedOutputStream will be blocked. And the write-operation thread will block until the read-PipedInputStream operation deletes the data from the buffer.
This means that the thread that writes the data to PipedOutputStream should not be the only thread responsible for reading the data from the corresponding PipedInputStream (so there are two threads open for read and write respectively). Assume that the T thread attempts to write 2000 bytes of data to the corresponding PipedOutputStream in a call to the PipedOutputStream write () method once, before the T thread blocks, It can write up to 1024 bytes of data (the size of the PipedInputStream internal buffer). However, once T is blocked, the operation to read PipedInputStream can no longer occur because T is the only thread that reads PipedInputStream, so that the T thread is completely blocked.
Object Flow
serialization, which has been made quite clear in this article, is primarily a simple way to simply cross the flow of objects.
Java provides ObjectInputStream, ObjectOutputStream these two classes for object serialization operations, these two classes are used to store and read the object's input and output stream class, as long as all the member variables in the object are stored, it is equal to save the object, You can then continue to use this object by reading the object from among the saved objects. ObjectInputStream, ObjectOutputStream can help developers to save and read the object member variable value of the process, but require read-write or stored objects must implement the Serializable interface.
To take a look at an example, let's start with an entity class person that implements the serializable interface:
PublicClass PersonImplementsserializable{/*** Serialization*/PrivateStaticFinalLong Serialversionuid = 7827863437931135333L;PrivateTransientString name;private int age; private final static String sex = "man" ; public person (String name, int age) {this.name = name; this.age = Age;} public String toString () { return "Name:" + this.name + ", Age:" + this.age + ", Gender:" + sex;}
Calling ObjectOutputStream and ObjectInputStream to write a serialization and deserialization method, I now have "Serializable.txt" under the D-Disk:
PublicStaticvoid Main (string[] args)Throwsexception{File File =New File ("D:/serializable.txt"); Serializable (file); deserializable (file);}//Serialization Object MethodsPublicStaticvoid serializable (file file)Throwsexception{OutputStream outputFile =NewFileOutputStream (file); ObjectOutputStream Oos =new ObjectOutputStream (outputFile); Oos.writeobject (New Person ("Zhang San");oos.close ();} // Deserialize object method public static void deserializable (file file) throws exception{InputStream Inputfile = new fileinputstream (file); ObjectInputStream ois = new objectinputstream (inputfile); Person p = (person) ois.readobject (); SYSTEM.OUT.PRINTLN (P);}
Now run, the D-Disk under a "Serializable.txt", the contents of the file is:
See garbled, because after the serialization itself is in accordance with a certain binary format of the organization of files, these binary formats can not be recognized by text files, so garbled is normal.
Of course, the console also has output:
Name:NULL, Age: 25, Gender: man
This proves that member variables modified by transient are not serialized.
Java IO7: Pipeline Flow, object flow