IO streams – Overview

I

Abstract classes InputStream and OutputStream form the basis of IO classes

Because byte stream oriented objects are not easy to process information stored in Unicode, classes that are specifically used to process Unicode characters are relayed from abstract classes reader and writer to form a separate hierarchy

The read and write operations of these classes are based on two byte Unicode symbols.

II

Each subclass only needs to override the read method in InputStream (read a byte) or the write method in OutputStream (write a byte to a location)

Both read and write will be blocked during execution until bytes are actually read in or written out. If the stream cannot be accessed immediately, the thread will be blocked (the number of bytes available for reading can be determined through the available method to avoid blocking)

API:

java. io. InputStream:

(1) Abstract int read(): reads a byte from the data and returns the byte. When it encounters the end of the stream, it returns - 1

(2) Int read (byte [] b): read in a byte array, return the actual number of bytes read in, read in b.length bytes at most, and return - 1 when encountering the end of the stream

(3) Int read (byte [] B, int off, int len): read in a byte array. This read method returns the number of bytes actually read, or - 1 when it hits the end of the stream

Parameter: B array of data read in

Off the offset in B of the position where the first read in byte should be placed

Len maximum number of bytes read in

(4) Long skip (long n): skip n bytes in the input stream and return the actual number of skipped bytes (it may be less than n if it touches the end of the stream).

(5) Int available(): returns the number of bytes available without blocking (blocking means that the current thread will lose its occupation of resources).

(6) Void close(): close the flow

(7) Void mark (int readlimit): mark the current position of the input stream (not all streams support this feature). If you have

If more than readlimit bytes are read in, the stream is allowed to ignore this flag.

(8) Void reset (): returns to the last tag, and the subsequent call to read will re read these bytes. If there are currently no tags,

The stream is not reset.

(9) Boolean marksupported(): returns true if the stream supports marking.

java. io. OutputStream:

(1) Abstract void write (int n): write out one byte of data.

(2) void write(byte[] b)

(3) Void write (byte [] B, int len): write out all bytes or a range of bytes into array B

Parameter: array written out by B data

Off the offset of the first write out byte in B

Len writes the maximum number of bytes

(4) Void close(): clear and close the output stream.

(5) Void flush(): clear the output stream, that is, send all buffered data to the destination

3、 Stream family:

(1) Byte stream family:

(2) Unicode text stream family

4、 Combined flow filter:

FileInputStream and fileoutputstream can only read bytes or byte arrays from the file (can not read numeric types), while datainputstream can only read numeric types (can not get data from the file). Then you can combine these two classes to read numbers from the file:

FileInputStream fin = new FileInputStream(“test.txt”);

DataInputStream din = new DataInputStream(fin);

Double s = din. readDouble();

Streams do not enter the buffer by default, so we can combine them as follows:

DataInputStream din = new DataInputStream(

New BufferedInputStream(

New FileInputStream(“test.txt”))); (the reason for putting datainputteam last is to use the method of datainputstream and hope that they can use the read method with buffering mechanism)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>