2019-5-29 Java learning diaries IO (byte stream)

IO streams Summary and Classification:

A: The concept

IO stream for processing data transfer between devices

java operations on the data stream by way of

java class for IO operations in stream packet

The flow is divided into two flows by: the input and output streams

According to the operation type is divided into two streams:

Byte stream: byte stream can operate any data, since any data in a computer is stored in the form of bytes

Character stream: a stream of characters can only be operated pure character data, more convenient

B: IO stream common parent class

Abstract parent class byte stream:

InputStream

OutputStream

Abstract parent class character streams:

Reader

Writer

C: IO program writing

Before use, import classes package IO

When in use, perform IO exception handling

After use, the release of resources

FileIputStream:

read () reads one byte

read () method returns a value Why is int?

read () method reads a byte is why returns int, instead of a byte

  Because the input stream of bytes can operate any type of file, such as pictures you audio, etc., these are the underlying documents stored in binary form, if each read returns byte, there are likely to encounter when read the middle 11111111

11111111 then this is the type of byte-1, our program is not encountered -1 will stop reading the following data is less than school, so receiving an int when read, if 11111111 will be supplemented in front 0 24 gather the 4 bytes, the byte type int -1 becomes the 255 type, which can ensure that the entire data read, that is of type int -1 and ends labeled

FileOutputStream:

write () method to write a byte time

FileOutputStream追加:

Copy of the picture:

FileInputStream read

FileOutputStream to write

Copies of the byte array available () Method:

Drawbacks: it is possible to run out of memory

Definition of small array:

Defining the standard format small array:

BufferedInputStream and BufferOutputStream copy:

A: Buffer thought

A first array of the byte stream read speed significantly faster than the time to read and write a byte of the velocity

This is added to the buffer effect of such an array, when java itself in the design,

Also take into account such a design, it provides a byte buffer flow

B:BufferedInputStream

BufferedInputStream a built-in buffer (array)

Reading a byte from the BufferedInputStream

BufferedInputStream will be a one-time read from the file 8192

 Until all of the buffers are used, only to re-read from the file 8192

C:BufferOutputStream

BufferOutputStream also a built-in buffer (array)

When the program write byte, not written directly to the file stream, first written to the buffer

Know when the buffer is full, BufferOutputStream will put the data in the buffer is written to a one-time file

D:Demo

E: small array of read-write and read with Buffered Which is faster?

If the definition of a small array size is 8192 bytes and then compared Buffered

Definition of small arrays will be slightly better, because read and write operations to the same array

The two arrays are operated Buffered

The difference between flush and close methods:

flush () method

Buffer used to refresh, refresh can write again

close () method

Close to release the flow of resources, methods, if it is with close stream object buffer () will not only close the stream, but also flushes the buffer before closing the stream, after closing can not write

Byte stream read and write Chinese:

Byte stream read Chinese problem

Chinese stream of bytes read when there could read half Chinese, resulting in garbled

Write byte stream problem of Chinese

Byte byte stream directly operated, it is necessary to write a Chinese string into a byte array

CRLF write write ( "\ r \ n" .getBytes ());

Standard flow exception handling code for version 1.6 and earlier:

Standard flow exception handling code for version 1.7:

Image Encryption:

Copy files:

Input data is copied to the file:

 

Guess you like

Origin www.cnblogs.com/clqbolg/p/10947341.html