Java I/O

emmmm,这篇文章的题目可能有点大了,以后会不断完善更新。

基础I/O

Java中和I/O相关的类都在java.io包里面(当然,还有java.nio),I/O的形式都是以流的形式进行的,大致的方法是读和写,对应的类(也是方向)是入和出,从入方向的流中读取数据时,其中所剩的数据越来越少,直至读完,所以一个流中只能进行一遍完整的读取过程。
最常用的就是字节流和字符串流。

字符串流

所有字符串流相关的类都继承自Reader和Writer,这是两个抽象类。它们的子类我能想起来的有FileReader和FileWriter,还有PrintWriter。

Reader中比较重要的方法有:
public int read(CharBuffer target) throws IOException
public int read() throws IOException
public int read(char[] cbuf) throws IOException
public abstract int read(char[] cbuf, int off, int len) throws IOException
public abstract void close() throws IOException

Writer中比较重要的方法有:
public void write(int c) throws IOException
public void write(char[] cbuf) throws IOException
public abstract int read(char[] cbuf, int off, int len) throws IOException
public void write(String str) throws IOException
public void write(String str, int off, int len) throws IOException
public Writer append(char c) throws IOException
public Writer append(CharSequence csq, int start, int end) throws IOException

字节流

所有字节流相关的类都继承自InputStream和OutputStream,这是两个抽象类。它们的子类很多,常见的有文件读写类FileInputStream和FileOutputStream,Servlet中用到的ServletInputStream和ServletOutputStream,套接字中用到的SocketInputStream和SocketOutputStream,websocket中用到的相关类。

InputStream中比较重要的方法:
public abstract int read() throws IOException
public int read(byte[] b) throws IOException
public int read(byte[] b, int off, int len) throws IOException
public int available() throws IOException
public void close() throws IOException

OutputStream中比较重要的方法:
public abstract void write(int b) throws IOException
public void write(byte[] b) throws IOException
public void write(byte[] b, int off, int len) throws IOException
public void flush() throws IOException
public void close() throws IOException

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值