java复习-I/O流

java复习
I/O流:
1
1)按数据流方向:
输入流,输出流;
2)按数据处理单位:
字节流,字符流;
3)按功能:
结点流,处理流;

2
java.io.*里面的所有流均是继承自以下四类;
在这里插入图片描述
3
结点流:直接从数据源读取数据;
处理流:在已存在的流的基础上,通过对已有的流的处理为程序提供更强大的读写能力;
4
InputStream

继承InputSteam的类用于向程序输入数据
输入数据的基本单位:字节(8bit)

基本方法:

int read( ) throws IOException 

int read(byte[ ] buffer) throws IOException

int read(byte[ ] buffer,int offset,int length)throws IOException

void close( ) throws IOException

注意:
read方法是阻塞方法
返回实际读取的字节数,到达文件结尾返回-1
5
OutputStream

继承OutputStream的类用于程序向外输出数据
数据的单位:字节(8bit)

基本方法:

int write(int b) throws IOException

int write(byte[ ] b) throws IOException

int write(byte[ ] b,int offset,int length)throws IOException

void flush( ) throws IOException

void close( ) throws IOException

flush将清空缓存,即将缓存的数据写到文件里,close是关闭文件 一般程序中,调用close( )前先调用flush(
),将缓冲区数据写入磁盘

6
Reader
继承Reader的类用于向程序中输入字符
数据的单位是字符(16bit)

基本方法

int read( ) throws IOException

int read(char[ ] cbuf) throws IOException

int read(char[ ] cbuf,int offset,int length) throws IOException

void close( ) throws IOException

7
Writer
继承Writer的类用于程序向外输出字符
数据的单位为字符(16bit)
基本方法

扫描二维码关注公众号,回复: 12787541 查看本文章
void write(int c) throws IOException

void write(char[ ] cbuf) throws IOException

void write(char[ ] cbuf,int offset,int length) throws IOException

void write(String string) throws IOException

void write(String string, int offset, int length) throws IOException

void close( ) throws IOException

void flush( )  throws IOException

8
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/timelessx_x/article/details/112058843