1. IO流分类
Java.io包(input / output)中包含多种流类型实现输入/输出功能,可从以下角度进行分类
1.1 按照数据流方向
输入、输出是以程序为参考
-
输入流
从数据源读取到程序 -
输出流
从程序写入到文件
1.2 按处理数据单位不同
- 字节流
读取单位为字节(Byte),每次读取8位(b) - 字符流
读取单位为字符,每次读取一个字符
1.3 按功能不同
- 节点流
直接从数据源读写数据 - 处理流
在已经存在的流上进行数据处理为程序提供更强大的读写功能
2. 继承关系
io包主要继承以下四个抽象类
字节流 | 字符流 | |
---|---|---|
输入流 | InputStream | Reader |
输出流 | OutputStream | Writer |
2.1 InputStream 基本方法
// Reads the next byte of data from the input stream.
// 读取一个字节,并且以整数形式返回,返回值范围(0~255)
// 当返回-1表示已经读取到流尾
abstract int read() throws IOException;
// Reads some number of bytes from the input stream
// and stores them into the buffer array b.
// 从输入流中读取多个字节,并存储到 数组b中
// 当返回-1表示已经读取到流尾
int read(byte b[]) throws IOException
// Reads up to len bytes of data from the input stream into an array of bytes.
// 读取len个字节,存在b数组中
// 当返回-1表示已经读取到流尾
int read(byte b[], int off, int len) throws IOException
// 关闭流,释放内存资源
void close()
// Skips over and discards n bytes of data from this input stream
// 跳过多少字节不读,返回实际跳过的自己数
long skip(long n) throws IOException
2.2 OutputStream
程序输出到文件,单位是字节Byte
// Writes the specified byte to this output stream.
// 写入字节数据
void write(int b) throws IOException
// Writes b.length bytes from the specified byte array to this output stream.
// 把一个字节数组写入文件
void write(byte[] b) throws IOException
// Writes len bytes from the specified byte array starting at offset off to this output stream.
// 将指定长度的字节数组从指定位置开始写入
void write(byte[] b,int off, int len) throws IOException
// 将输出流中的数据写入目的地
void flush() throws IOException
// 关闭流,释放内存资源
void close()
2.3 Reader
继承自Reader的流,都是用于向程序中读入数据,且数据的单位为字符(16 bit)
// Reads a single character.
// 读取一个字符并返回整数形式
// 如果返回 -1 读取到流尾
int read() throws IOException
// Reads characters into an array.
// 读取字符存到cbuf数组
// 返回-1表明读取到流尾
int read(char[] cbuf) throws IOException
// Reads characters into a portion of an array
int read(char[] cbuf, int off, int len) throws IOException
// 关闭流,释放资源
void close() throws IOException
2.4 Writer
继承自Writer流都用于写入数据,且数据单位为字符(16 bit)
// Writes a single character.
void write(int c)
// Writes an array of characters.
void write(char[] cbuf)
// Writes a portion of an array of characters.
abstract void write(char[] cbuf,int off, int len) throws IOException
// Writes a string.
void write(String str) throws IOException
// 关闭
abstract void close() throws IOException
// 将输出流中的数据写入目的地
abstract void flush() throws IOException