(reprint) Java:io Flow Learning Summary

Source: Internet
Author: User

Today just read the Java IO Stream operation, the main context looked over, can not guarantee the use of later can be handy, but at least when used to know that there is such a function can be achieved, the following for a brief summary of learning:

IO flow is mainly used in hard-core, memory, keyboard and other processing equipment data operation, according to the data type of processing data can be divided into: byte stream (abstract base class is InputStream and OutputStream) and character stream (abstract base class for reader and writer).  Depending on the flow direction, it can be divided into: input stream and output stream. The main structure can be used to indicate:

The main differences between character streams and byte streams are:

1. When the byte stream is read, a byte is returned, and the character stream is read to one or more bytes using the byte throttle (the number of bytes in Chinese corresponds to two, which is 3 bytes in the UTF-8 Code table). First check the specified encoding table and return the found characters.

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

  

The IO stream can be divided into two main categories: node flow and processing flow.

One, node flow type

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

Type Character Stream BYTE stream
File (Files) Filereaderfilewriter Fileinputstreamfileoutputsream
Memory Array Chararrayreaderchararraywriter Bytearrayinputstreambytearrayoutputsream
Memory String Stringreaderstringwriter -
Pipe (piping) Pipedreaderpipedwriter Pipedinputsreampipedoutputsream

  ii. processing Flow typeThe type is the connection and encapsulation of an existing stream, which implements the data read and write through the function invocation of the encapsulated stream, and the process flow is always constructed with a different stream object as an argument, and a stream object passes through the other stream's multiple wrappers, called the stream's link. The main can be divided into the following types:1. Buffer Stream (Bufferedinputstream/bufferedoutputstream and Bufferedwriter/bufferedreader)He can improve the efficiency of convection operation. Write Buffer object: [Java]View PlainCopy
    1. BufferedWriter bufw=New BufferedWriter (new FileWriter ("Buf.txt"));
Read Buffer object: [Java]View PlainCopy
    1. BufferedReader bufr=New BufferedReader (new FileReader ("Buf.txt"));
There is a unique method for this type of flow: ReadLine (), reading one line at a time, returning the character data before the row tag as a string, returning NULL when read to the end, or the Read method of the stream object associated with the buffer, except that every time it is read to a character, The temporary storage is not performed first, and the data stored in the temporary container is returned one time when the carriage return tag is read.2. Conversion Stream (Inputstreamreader/outputstreamwriter)The type is a bridge between the byte stream and the character stream, which can encode the encoded translation of the bytes read into the data. The main constructors are: [Java]View PlainCopy
    1. InputStreamReader (InputStream);  //Through the constructor initialization, using the system is the default encoding table GBK.
    2. Inputstreamwriter (inputstream,string charSet);  //Through this constructor initialization, you can specify the encoding table.
    3. OutputStreamWriter (OutputStream);  //initialized by this constructor, using the system's default encoding table GBK.
    4. OutputStreamWriter (outputstream,string charSet);  //Through this constructor initialization, you can specify the encoding table.
Note:When using FileReader to manipulate text data, the object uses the default encoding table, which is FileReader fr=new filereader ("A.txt");   With InputStreamReader isr=new InputStreamReader (New FileInputStream ("A.txt")); Have the same meaning. If you want to encode a table using the specified table, you must use a transform stream, that is, if the character data in the file in A.txt is encoded by UTF-8, then the encoding table must be specified when reading, so it is necessary to convert the stream. namely: InputStreamReader isr=new InputStreamReader (New FileInputStream ("A.txt"), utf-8);3. Data stream (Datainputstream/dataoutputstream)This data stream can easily store and read some basic types of data, without further conversion, usually as long as the data of the basic data type is manipulated, it needs to be packaged by DataStream. Construction Method: [Java]View PlainCopy
    1. Datainputstreamreader (InputStream);
    2. Datainputstreamwriter (OutputStream);
Examples of methods: [Java]View PlainCopy
    1. int readInt ();//Read four bytes at a time and turn it into an int value
    2. Writeint (int);//write four bytes at a time, note that unlike write (int), write (int) writes only the lowest one 8 bits of the integer, and the remaining three 8 is the missing
    3. Hort Readshort ();
    4. Writeshort (short);
    5. String readUTF ();  //Read characters according to Utf-8 modified version, note that it can only read writeUTF () write character data.
    6. writeUTF (String);  //In accordance with Utf-8 modified version of the character data stored, can only be read by readUTF.
Note:When using the data stream to read/save data, it is necessary to have a certain order, that is, a type of data written first must be read first, obey the principle of FIFO.Four, print flow (printstream/printwriter)PrintStream is a byte print stream, System.out corresponds to a type of 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 to True, only 3 methods can be used: println, Printf,format.v. Object Flow (Objectinputstream/objectoutputstream)This type of flow can access the class as a whole, the main method is: Object readobject (); The method throws an exception: Classnotfountexception. void WriteObject (Object): The object being written must implement an interface: Serializable, otherwise it will be thrown: notserializableexception

(reprint) Java:io Flow Learning Summary

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.