类关系图
简介
包路径:java.io
说明:OutputStream,字节出流,用于将内容通过流的方式从应用程序输出到外部设备,此抽象类是**表示字节输出流的所有类的超类**。为字节输出流提供一个标准和基本的方法及简单的实现,子类可以根据自己的特点进行重写和扩展。
OutputStream中有一个抽象方法write(),是字节输出流的核心方法,要求子类必须实现此方法
特点:1、以字节为单位处理数据,每次读取一个字节,适合处理二进制文件,如音频、视频等
2、不能有效解决中文乱码的问题,且效率低下
源码分析
package java.io;
/**
* @author Arthur van Hoff
* @see java.io.BufferedOutputStream
* @see java.io.ByteArrayOutputStream
* @see java.io.DataOutputStream
* @see java.io.FilterOutputStream
* @see java.io.InputStream
* @see java.io.OutputStream#write(int)
* @since JDK1.0
*/
public abstract class OutputStream implements Closeable, Flushable {
/**
* 往流中写(输出)一个字节b
* @param b the <code>byte</code>.
*/
public abstract void write(int b) throws IOException;
/**
* 往流中写(输出)一个字节数组b
* @param b the data.
* @see java.io.OutputStream#write(byte[], int, int)
*/
public void write(byte b[]) throws IOException {
write(b, 0, b.length);
}
/**
* 把字节数组b中从下标off开始,长度为len的字节写入流中
* @param b the data.
* @param off the start offset in the data.
* @param len the number of bytes to write.
*/
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;
}
// 遍历调用write方法写入
for (int i = 0 ; i < len ; i++) {
write(b[off + i]);
}
}
/**
* 刷空输出流,并输出所有被缓存的字节,由于某些流支持缓存功能,该方法将把缓存中所有内容强制输出到流中。
*/
public void flush() throws IOException {
}
/**
* 关闭当前流、释放所有与当前流有关的资源
*/
public void close() throws IOException {
}
}
总结
1、OutputStream,字节出流,用于将内容通过流的方式从应用程序输出到外部设备,此抽象类是**表示字节输出流的所有类的超类**。为字节输出流提供一个标准和基本的方法及简单的实现,子类可以根据自己的特点进行重写和扩展。
2、OutputStream中有一个抽象方法write(),是字节输出流的核心方法,要求子类必须实现此方法