java输出流的高层抽象:java.io.OutputStream
此抽象类是表示输出字节流的所有类的超类。输出流接受输出字节并将这些字节发送到某个接收器。
需要定义 OutputStream 子类的应用程序[b]必须始终提供至少一种可写入一个输出字节的方法[/b]。
OutputStream 实现了两个接口:
1、Closeable:定义输出流需要关闭操作。关闭此输出流并释放与此流有关的所有系统资源。
2、Flushable:定义输出流需要刷新此输出流并强制写出所有缓冲的输出字节。
特点:[b]和java.io.InputStream一样,他的底层就是byte数组的各自操作[/b]
我们来瞅瞅它的定义吧。
此抽象类是表示输出字节流的所有类的超类。输出流接受输出字节并将这些字节发送到某个接收器。
需要定义 OutputStream 子类的应用程序[b]必须始终提供至少一种可写入一个输出字节的方法[/b]。
OutputStream 实现了两个接口:
1、Closeable:定义输出流需要关闭操作。关闭此输出流并释放与此流有关的所有系统资源。
2、Flushable:定义输出流需要刷新此输出流并强制写出所有缓冲的输出字节。
特点:[b]和java.io.InputStream一样,他的底层就是byte数组的各自操作[/b]
我们来瞅瞅它的定义吧。
public abstract class OutputStream implements Closeable, Flushable {
/**
* Writes the specified byte to this output stream. The general
* contract for <code>write</code> is that one byte is written
* to the output stream. The byte to be written is the eight
* low-order bits of the argument <code>b</code>. The 24
* high-order bits of <code>b</code> are ignored.
写如果特定的byte到 当前的OutputStream输出流,对于int 占用4个字节,只会写入低8位,其他忽略掉。
* <p>
* Subclasses of <code>OutputStream</code> must provide an
* implementation for this method.
*
* @param b the <code>byte</code>.
* @exception IOException if an I/O error occurs. In particular,
* an <code>IOException</code> may be thrown if the
* output stream has been closed. 特别:如果已经关闭再写入,会抛出IOException异常。
*/
public abstract void write(int b) throws IOException;
/**
*写入b数组的所有数据
*/
public void write(byte b[]) throws IOException {
write(b, 0, b.length);
}
/**
* 把b的字节数组中的数据,从b[off] 到b[off+len-1] 逐个的按顺序的写入到输出流
* 这个方法是循环调用write(int)方法,子类最好能覆盖此方法以提供更有效的实现
*
* @param b the data.
* @param off the start offset in the data. 代表从b 字节数组的何处开始取数据
* @param len the number of bytes to write. 从b字节数组取 len长度的数据
*
*/
public void write(byte b[], int off, int len) throws IOException {
if (b == null) {
throw new NullPointerException();
} else if ((off < 0) || (off > b.length) || (len < 0) ||
((off + len) > b.length) || ((off + len) < 0)) {
throw new IndexOutOfBoundsException();
} else if (len == 0) {
return;
}
for (int i = 0 ; i < len ; i++) {
write(b[off + i]);
}
}
/**
*【jdk解释:】
刷新此输出流并强制写出所有缓冲的输出字节。flush 的常规协定是:如果此输出流的实现已经缓冲了以前写入的任何字节,则调用此方法指示应将这些字节立即写入它们预期的目标。
如果此流的预期目标是由基础操作系统提供的一个抽象(如一个文件),则刷新此流只能保证将以前写入到流的字节传递给操作系统进行写入,但不保证能将这些字节实际写入到物理设备(如磁盘驱动器)。
*/
public void flush() throws IOException {
}
/**
* 关闭此输出流并释放与此流有关的所有系统资源。close 的常规协定是:该方法将关闭输出流。关闭的流不能执行输出操作,也不能重新打开。
*/
public void close() throws IOException {
}
}