Summary of IO streaming learning in Java

Source: Internet
Author: User

Today, just read the Java IO Stream operation, the main context looked at again, can not guarantee the use of later can be handy, but at least when you know there is such a function can be achieved, the following to learn a simple summary:

IO flow is mainly used in hard PCB, memory, keyboard and other processing equipment on the data operations, according to the processing of data types can be divided into: byte stream (abstract base class for InputStream and OutputStream) and character streams (abstract base class for reader and writer).  According to the flow of different, can be divided into: input flow and output stream. The main structure can be represented in the following illustration:

the main differences between character streams and byte streams:

1. When a byte stream is read, a byte is returned; the character stream reads one or more bytes with a byte (the number of bytes in Chinese is two, in the UTF-8 Code table is 3 bytes). First check the specified code table and return the characters you have found.

2. The byte stream can handle all types of data, such as: Picture, Mp3,avi video file, and character streams can only handle characters data. As long as you are working with plain text data, you need to prioritize the use of character streams, in addition to byte throttling.

  

io flow can be divided into two main categories: node flow and processing flow.

One, node flow type

The type can read or write data from or to a specific location or node. The main types are as follows:

Type Character streams Word throttling
File (Files) FileReader FileWriter FileInputStream Fileoutputsream
Memory Array CharArrayReader Chararraywriter Bytearrayinputstream Bytearrayoutputsream
Memory String StringReader StringWriter -
Pipe (pipe) Pipedreader PipedWriter Pipedinputsream Pipedoutputsream

ii. Handling Flow types This type is the connection and encapsulation of an existing stream, through the encapsulation of the flow of the function of the call to achieve data read and write, the processing flow of the construction method is always to bring a other flow object as a parameter, a stream object into the other flow of multiple packaging, called the flow of the link. The main can be divided into the following: 1, buffer flow (Bufferedinputstream/bufferedoutputstream and Bufferedwriter/bufferedreader) he can improve the operational efficiency of convection. Write Buffer object:

BufferedWriter bufw=new BufferedWriter (New FileWriter ("Buf.txt"));  

Read Buffer object:
BufferedReader bufr=new BufferedReader (New FileReader ("Buf.txt"));  

The stream of this type has a unique method: ReadLine (); Once a row is read, the character data before the row tag is returned as a string, and when the end is read, NULL is returned, whether it is the Read method of the stream object associated with the buffer, except that each time a character is read,                                Do not do a specific operation, the first temporary storage, when reading to the carriage return mark, the temporary container stored in a one-time return of data.       2. Conversion Stream (Inputstreamreader/outputstreamwriter) a bridge between a byte stream and a character stream that can encode a coded conversion of bytes of data that is read. The constructor functions are mainly:
InputStreamReader (InputStream);        Initialized by the constructor, using the default encoding table GBK for this system.  
 Inputstreamwriter (inputstream,string charSet);   Initialized by this constructor, you can specify the encoding table.  
 OutputStreamWriter (outputstream);      Initialized by this constructor, using the default encoding table GBK for this system.  
 OutputStreamWriter (outputstream,string charSet);   Initialized by this constructor, you can specify the encoding table.  

Note:When using FileReader to manipulate text data, the object uses the default encoding table, that is, the FileReader fr=new filereader ("A.txt");   With InputStreamReader isr=new InputStreamReader (New FileInputStream ("A.txt")); The same meaning. If you want to use the specified table encoding table, you must use a conversion stream, that is, if the character data in the file in A.txt is encoded by utf-8, then you must specify the encoding table when you read it, and then the stream must be converted.            namely: InputStreamReader isr=new InputStreamReader (New FileInputStream ("A.txt"), utf-8); 3, the data stream (Datainputstream/dataoutputstream) This data flow can easily to some basic types of data directly stored and read, do not need further conversion, usually as long as the operation of the basic data type data, you need to pass the Datastrea           M for packing. Construction Method:
Datainputstreamreader (InputStream);  
 datainputstreamwriter (OutputStream);  

Methods for example:
int readInt ();//reads four bytes at a time and converts it to an int value  
writeint (int);//write four bytes at a time, note that, unlike write (int), write (int) writes only the lowest 8 bits of the integer. The remaining three 8 for lost  
Hort Readshort ();  
Writeshort (short);  
String readUTF ()//reads characters in Utf-8 modified version, noting that it can read only the character data written by writeUTF ().  
 writeUTF (String),//Utf-8 modified Edition to store character data, can only be read through readUTF.  

Note:When using data stream to read/save data, it is necessary to have a certain order, that is, some type of data must first read out, obey the principle of FIFO. The print stream (Printstream/printwriter) PrintStream is a byte print stream, System.out the corresponding type is PrintStream, and its constructor can accept values of three data types: 1. String path. 2.File Object 3.OutputStream PrintStream is a character print stream whose constructors can accept four types of values: 1. String path. 2.File Object 3.OutputStream 4.Writer for 1, 2 types of data, you can specify the encoding table, that is, the character set, for 3, 4 types of data, you can specify automatic refresh, when the automatic refresh is true, only 3 methods can be used: println,   Printf,format. V. Object Flow (Objectinputstream/objectoutputstream) The stream of this type can access the class as a whole, the main methods are: Object ReadObject (); The method throws an exception: Classno         Tfountexception. void WriteObject (Object): The objects being written must implement an interface: Serializable, or it will be thrown: notserializableexception
Note: article reference: http://blog.csdn.net/zzp_403184692/article/details/8057693
Related Article

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.