本文目录:
Byte Streams
Java流分为: byte streams和character streams. I/O是基于文本(text-based)或数据(data-based, 二进制)的.
Byte Streams: byte是8位的. 基于数据的I/O二进制流, 如一幅图像的位数据. Byte streams被称为input streams和output streams. 对于每个input stream都有相应的output stream.
Character Streams: character是16位的UTF-16字符, 基于文本的I/O流是人们可读的字符, 如程序的源代码. Charater streams被称为readers和writers. 对于多数的input或output streams都有相应的reader或writer字符流, 反之亦然.
在java.io包中, 抽象类InputStream和OutputStream是其他byte streams(如FileInputStream, FileOutputStream)的父类. 如下图:
下面介绍抽象类InputStream和OutputStream中的方法及其用法.
public abstract int read() throws IOException
作用: 读取一个字节并返回它.
说明: 返回值的范围为0-255. 若遇到流的末尾, 则返回-1. 如果仅考虑返回一个byte值, 返回类型为byte即可. 但考虑到要返回一个标志-1来表示流的末尾, 故返回类型用了表示范围更大的int型.
eg.
package mjn.io;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* count the bytes of a text file
* @author MJN
* @date 2011-09-30
*/
public class CountBytes {
public static void main(String[] args) throws IOException {
InputStream in = new FileInputStream("in.txt");
int total = 0;
while (in.read() != -1) {
total++;
}
System.out.println(total + " bytes.");
}
}
文本文件
in.txt的内容如下:
hello world!
程序输出
:
12 bytes.
public int read(byte[] buf, int offset, int count) throws IOException
作用: 从字节流中读取count个字节到数组buf(从buf[offset]至buf[offset + count - 1]), 返回实际读取的字节个数.
说明: 读取的字节个数最大值是count, 数组buf只有buf[offset]至buf[offset + count - 1]被改变, buf的其他值不变. 若没有字节可读(遇到流的末尾), 方法返回0.
public int read(byte[] buf) throws IOException
作用: 执行read(buf, 0, buf.length).
public long skip(long count) throws IOException
作用: 向后跳过count个字节, 返回实际跳过的字节数
说明: 若count为负数, 则向前跳.
public long available() throws IOException
作用: 返回可读(或可跳过)的字节数without blocking.
eg.
package mjn.io;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* count the bytes of a text file
* with available method
*
* @author MJN
* @date 2011-09-30
*/
public class CountBytes1 {
public static void main(String[] args) throws IOException {
InputStream in = new FileInputStream("in.txt");
long total = in.available();
System.out.println(total + " bytes.");
}
}
文件in.txt中的内容及程序输出与上面的read()
方法中的相同.
public void close() throws IOException
作用: 关闭输入流.
说明: 此方法用于释放相应的资源. 如果一个流被关闭, 再对此流进行操作, 则会抛出IOException异常. 关闭一个已经关闭的流, 没有任何的效果(不会抛出异常).
public abstract void write(int b) throws IOException
作用: 将b作为一个字节写入流.
说明: 参数是int型而不是byte, 是因为它通常是byte型数据算术运算的结果. 涉及到byte的表达式类型是int型, 所以就不必再手动转换为byte了(如果参数是byte型, 就要用强制类型转换cast到byte). 只有int型中的低8位被写入流.
public void write(byte[] buf, int offset, int count) throws IOException
作用: 将字节数组buf中的count个字节写入流(从buf[offset]到buf[offset + count - 1])
public void write(byte[] buf) throws IOException
作用: 执行write(buf, 0, buf.length)
public void flush() throws IOException
作用: 刷新流.
说明: 如果流中缓冲了一些字节(write方法引起的), 则立即将字节写入目的地. 如果目的地是另一个流, 则也被刷新. 一次刷新会引起一系列流的刷新. 如果流没有缓冲, 则什么也不做. 此方法被定义在接口Flushable中.
public void close() throws IOException
作用: 关闭输出流.
说明: 此方法用于释放相应的资源. 如果一个流被关闭, 再对此流进行操作, 则会抛出IOException异常. 关闭一个已经关闭的流, 没有任何的效果(不会抛出异常).
References:
[1] 《The Java Programming Language, Fourth Edition》