Logical relationship of IO stream in Java

Source: Internet
Author: User

In InputStream and OutputStream, the following IO processing objects are available for different sources of information:

1) Bytearrayinputstream and Bytearrayoutputstream objects in bytes, read and write to the contents of the memory buffer

2) Read and write the StringBufferInputStream and Stringbufferoutputstream objects of string data with strings as objects.

3) FileInputStream and FileOutputStream objects for file read and write operations.

4) PipedInputStream and PipedOutputStream objects for pipeline data read and write operations.


The above-mentioned IO classes, from the literal above, can accommodate data read and written from different devices, but their functionality is not limited to this. They are more like pipes connected to each device, and we can use the new statement to create a link channel with a memory buffer, and to perform read and write operations through the Read method of type input and the write method of the output type.


Accordingly, in order to accommodate 16-bit Unicode encoding, JDK1.1 provides the following IO objects for reader and writer:

1) CharArrayReader and Chararraywrite objects used to read and write memory buffer data areas, which correspond to Bytearrayinputstream and Byteoutputstream.

2) StringReader and Stringwrite objects used to read and write data of type string, which correspond to StringBufferInputStream and Stringbufferoutputstream.

3) FileReader and FileWrite objects used to read and write file data, which correspond to FileInputStream and FileOutputStream.

4) Pipedreader and Pipedwrite objects used for pipeline operations, which correspond to PipedInputStream and PipedOutputStream.


It is worth noting that the IO operation method of the pipeline, mainly used in the thread communication in Linux, has less chance to use under Windows.


We can actually complete the IO operation using the above input and output (or reader and writer objects), but we will find that it is not enough. For example, we get the data stream from the buffer, but we can't read the basic types of data such as int and char more accurately. As another example, when we read data from a file, we cannot read the data in a certain location immediately and accurately.

We should not encapsulate the above auxiliary "functions" in the Io stream object, because doing so will allow the IO stream to take care of the operations of other types of logic outside the responsibility of reading and writing data. Therefore, in the Java IO class, there is also a class that is responsible for "optimizing the IO Stream function" of the "adorner". These adorner classes from the input and output angle, can be divided into two types of inputs and outputs, from the processing of the character set angle, can be divided into stream and reader two categories.


Different decorators can do different jobs:

1) DataInputStream and DataOutputStream can read data from the stream into basic types (such as int, etc.) and, through the ReadByte and Readdouble methods they provide, can read directly from the buffer to a specific type of data. Similarly, BufferedReader and bufferedwriter are used to read basic types of data from reader and writer.

2) Bufferedinputstreami and Bufferedoutstream encapsulate the method of reading and writing the specified amount of data in the various types of buffers, and similarly, BufferedReader and BufferedWriter are the classes they correspond to.

3) PrintStream can produce output in the specified format, and PrintWriter is its corresponding class. It can be used to write data such as int directly into the buffer.

4) The Linenumberinputstream object can be used to count the number of rows in the input stream object, and LineNumberReader is its corresponding class.

5) The Randomaccessfile class can be used to read data from specific locations in the specified file.

6) The Streamtokenizer class is used to divide the input stream into several blocks of text based on the inputstream inside the partition as tokens.


Decoration classes do not work independently, they simply provide the icing on the cake for specific IO operations, and by using these decorations, we can greatly simplify the task of reading and writing specific devices in Java.


Now to summarize, the above mentioned three types of Io objects, which are InputStream and OutputStream type only can accommodate 8 byte-encoded objects, reader and writer can accommodate 16 byte-encoded objects and to improve the input and output operation function "Decorate" class.


The following is a code for reading and writing memory data

Import Java.io.ioexception;//import Java.io.stringbufferinputstream;import Java.io.stringreader;import Java.io.bufferedoutputstream;import Java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.printstream;import Java.io.stringreader;public class IOExample1 {public static void main (string[] args) {try{ String meminput= "Input into memory!"; StringReader sin=new StringReader (meminput); int Singleword; PrintStream outtofile=new PrintStream (New Bufferedoutputstream (New FileOutputStream ("D:\\1.out"));//through loops, writing files while ((Singleword=sin.read ())!=-1) {outtofile.print (char) singleword); System.out.print ((char) singleword);} Sin.close ();//force output to buffer Outtofile.flush (); Outtofile.close ();} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();}}}



Logical relationship of IO 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.