Java Foundation--io

Source: Internet
Author: User

1. Understanding and classification of streams

The IO class architecture of 2,java processing stream

3, byte stream and character stream conversion

Overview of 4,nio

5,java IO shutdown Resource

1. Understanding and classification of streams

A stream is a set of sequences of bytes that have a starting point and an end point, a generic or abstract of the data transfer.

Depending on the flow direction, we are divided into input and output streams

Based on the data type structure in the flow, we divide into character stream and byte stream.

BYTE stream Character Stream
A read-in or read-out is a 8-bit binary. A read-in or read-out is a 16-bit binary.
operates directly on files. There is a buffer between the stream and the file. Character stream streams are independent of file

The following program verifies the existence of a buffer.

BYTE Bytedemo

New File ("Bytedemo_01.txt"null= "helloworld!!" ; Try {    new fileoutputstream (file);   gets the output byte stream      catch  (IOException e) {    e.printstacktrace ();}
View Code

Bytedemo_01.txt content:

Character Stream: chardemo_01

Null= "helloworld!!" ; Try {    new  FileWriter (file);    Out.write (str);} Catch (IOException e) {    e.printstacktrace ();}
View Code

At this point the Chardemo_01.txt content is empty, the buffer is not flushed, the characters are not written to the file, the Out.write (str), the statement is added Out.flush (), and then the characters are written to the file.

The IO class architecture of 2,java processing stream

The superclass of Java processing byte stream is inputstream,outputstream. Common methods See API,

In order to compensate for the lack of some application scenarios where the byte stream does not have buffers, Java provides two ways to improve the InputStream

          • Making the program improvements directly on the InputStream, we can customize the buffer array to read the content first into its own buffer array.

int read (byte[] b, int off, int len)//read byte stream into B array, off is the starting position of B array, Len is the length of the read-in, off+len<=b the length of the array

Byte[] B = new byte[1024];

in = new FileInputStream (file);

In.read (b,0,1024);// write 1024 bytes of data to B from b[0]

          • Using the Decorator class FilterInputStream, its subclass Bufferedinputstream encapsulates the buffer array on the program implementation, the default size is 8192

Bufferedinputstream bis = new Bufferedinputstream (in)

The same form as above.

In access efficiency, the first method of designing a buffer array with a length greater than or equal to 8192 is similar to the second access efficiency. Just the second method provides the Mar () and Reset () methods, enriching the methods we can use to access the data

Reader,writer of the superclass of Java processing character streams

Java handles character streams by default, with a buffer, similar to Bufferedinputstream, in which the character stream remembers to flush the buffer in a timely manner while reading the write, preventing the buffer from overwriting.

3, byte stream and character stream conversion 

Before you introduce a transformation, you need to understand the decoding and encoding

Decoding: string-->byte[] str.getbbytes ("GBK")//decoding the original string in GBK form

Code: byte[]-->string String str = new String (b, "GBK")//The original byte encoding is assigned to STR in GBK form

The subclass InputStreamReader of reader and the subclass of writer OutputStreamWriter provide conversion of byte stream and character stream.

InputStreamReader

BYTE stream---> character stream. is a bridge of bytes flowing to a character stream. If you do not specify a character set encoding, the decoding process uses the default character encoding for the platform, such as: GBK.

InputStreamReader ISR = new InputStreamReader (new InputStream (), Charset CS);//

Apply: It is used in the form of a byte stream when the text content is initially acquired in some applications.

OutputStreamWriter

Character Stream---> byte stream. The characters that are to be written to the stream can be encoded into bytes using the specified charset.

OutputStreamWriter (OutputStream out, Charset CS)

For input that accepts only processing byte streams, you need to write the character stream of the text file or content.

Overview of 4,nio

NIO is the new IO processing mechanism of Java, the biggest difference is that the NIO convection implements the buffer mechanism, unlike the traditional IO, which is implemented from the programmer's encapsulation, it is more efficient than the underlying operating system.

5,java IO shutdown Resource

Take the input character stream as an example

FileReader FR = null;
FileWriter FW = NULL;

Try{}catch () {}

finally{

try{

if (null! = FR) {

Fr.close ();

}

}catch (IOException e) {

throw new RuntimeException ("Shutdown failed");
}

try{

if (null! = FW) {

Fr.close ();

}

}catch (IOException e) {

throw new RuntimeException ("Shutdown failed");
}

}//finally

Java Foundation--io

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.