cool hit counter The four basic abstract classes of IO streams InputStream, OutputStream, Reader, Writer_Intefrankly

The four basic abstract classes of IO streams InputStream, OutputStream, Reader, Writer


Copyright: This is an original post by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/huyuyang6688/article/details/43916877

The four abstract classes in the java.io package and their relationships were briefly understood above, and they and their subclasses are further summarized here.

At the end of the last blog, the entire IO family in Java was posted, and then I reworked it a bit to identify node streams and processing streams (green for node streams and yellow for processing streams) based on the previous one.

Let's talk about each of these four abstract classes here.

A careful observer might notice that most input and output streams are paired, and where there is an input, there is an output, one to the other.

InputStream

The role of the input byte stream InputStream is to flag classes that produce input from different data origins.

The basic methods are.

intread()approach: Read a byte, Return as an integer(0~255), If the return-1, then it has reached the end of the input stream;

intread(byte[ ] b)approach: Reads a series of bytes and stores them into an arrayb in, Returns the actual number of bytes read, If you are already at the end of the input stream before reading, return-1;

intread(byte[ ] b,int off,int len)approach: Read up tolen bytes and store them in a byte arrayb in, Returns the actual number of bytes read, If you are already at the end of the input stream before reading, return-1;

void close() method: closes this input stream and releases the system resources associated with the stream.

long skip(long n) method: skip n bytes without reading, jump directly after n bytes, return the actual number of bytes skipped.

Definition required InputStream Subclassed applications must always provide a method to return the next input byte.

OutputStream

The output byte stream OutputStream accepts output bytes and sends them to some receiver.

The basic methods are.

void write(int b) method: writes the specified bytes to this output stream.

void write(byte[ ] b) method: writes b.length bytes from the specified byte array to this output stream.

void write(byte[ ] b,int off,int len) method: writes len bytes from the specified byte array, starting at offset off, to this output stream.

void close() method: closes this output stream and releases the system resources associated with this stream

void flush() method: flushes this output stream and forces all buffered output bytes to be written.

Definition requiredOutputStream Subclassed applications must always provide at least one method that can write to an output byte.

Reader

The input character stream Reader defines methods for reading data in characters.

The basic methods are.

int read() method: reads a character and returns it as an integer (0~255), or -1 if it has reached the end of the input stream.

int read(char[ ] cbuf) method: reads a series of characters and stores them in an array b, returning the actual number of characters read, or -1 if the end of the input stream has been reached before the read.

int read(char[ ] cbuf,int off,int len) method: reads up to len characters and stores them in an array cbuf, returning the actual number of bytes read, or -1 if the end of the input stream has been reached before reading.

void close() method: closes this input stream and releases the system resources associated with the stream.

long skip(long n) method: skip n characters without reading, skip directly after n bytes, return the actual number of characters skipped.

The only methods that must be implemented by subclasses are read(char[], int, int) and close(). However, most subclasses will override some of the methods defined here to provide greater efficiency and/or other functionality.

Writer

The output character stream Reader defines methods for writing data in characters.

The basic methods are.

void write(int c) method: writes a character c to the output stream.

void write(char[ ] cbuf) method: writes an array of characters cbuf to the output stream.

void write(String str) method: writes the string str to the output stream.

void write(char[ ] cbuf,int off,int len) method: writes len characters from the specified string array cbuf, starting at offset off, to this output stream.

void write(String str,int off,int len) method: writes len characters of the specified string str starting from offset off to this output stream.

The only methods that must be implemented by subclasses are write(char[], int, int), flush(), and close(). However, most subclasses will override some of the methods defined here to provide greater efficiency and/or other functionality.

Although each of these four abstract classes defines the appropriate methods, it is their subclasses that are really used when reading and writing data, and the subclasses usually redefine the relevant methods. Examples of some basic, commonly used classes will be given later in practice.


Recommended>>
1、Handson machine learning tuning
2、A Practical Guide to ScikitLearn and TensorFlow Machine Learning Chapter 10 Artificial Neural Networks
3、Left hand with R right hand Python series dynamic web crawling with selenium driven browser
4、Solution for file not being edited after atom editor starts
5、IntellijIDEA artifacts actually have these hacks

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号