类关系图

图片.png

简介

包路径java.io

说明:OutputStream,字节出流,用于将内容通过流的方式从应用程序输出到外部设备,此抽象类是**表示字节输出流的所有类的超类**。为字节输出流提供一个标准和基本的方法及简单的实现,子类可以根据自己的特点进行重写和扩展。

OutputStream中有一个抽象方法write(),是字节输出流的核心方法,要求子类必须实现此方法

特点:1、以字节为单位处理数据,每次读取一个字节,适合处理二进制文件,如音频、视频等
2、不能有效解决中文乱码的问题,且效率低下

源码分析

  1. package java.io;
  2. /**
  3. * @author Arthur van Hoff
  4. * @see java.io.BufferedOutputStream
  5. * @see java.io.ByteArrayOutputStream
  6. * @see java.io.DataOutputStream
  7. * @see java.io.FilterOutputStream
  8. * @see java.io.InputStream
  9. * @see java.io.OutputStream#write(int)
  10. * @since JDK1.0
  11. */
  12. public abstract class OutputStream implements Closeable, Flushable {
  13. /**
  14. * 往流中写(输出)一个字节b
  15. * @param b the <code>byte</code>.
  16. */
  17. public abstract void write(int b) throws IOException;
  18. /**
  19. * 往流中写(输出)一个字节数组b
  20. * @param b the data.
  21. * @see java.io.OutputStream#write(byte[], int, int)
  22. */
  23. public void write(byte b[]) throws IOException {
  24. write(b, 0, b.length);
  25. }
  26. /**
  27. * 把字节数组b中从下标off开始,长度为len的字节写入流中
  28. * @param b the data.
  29. * @param off the start offset in the data.
  30. * @param len the number of bytes to write.
  31. */
  32. public void write(byte b[], int off, int len) throws IOException {
  33. // 合法性判断
  34. if (b == null) {
  35. throw new NullPointerException();
  36. } else if ((off < 0) || (off > b.length) || (len < 0) ||
  37. ((off + len) > b.length) || ((off + len) < 0)) {
  38. throw new IndexOutOfBoundsException();
  39. } else if (len == 0) {
  40. return;
  41. }
  42. // 遍历调用write方法写入
  43. for (int i = 0 ; i < len ; i++) {
  44. write(b[off + i]);
  45. }
  46. }
  47. /**
  48. * 刷空输出流,并输出所有被缓存的字节,由于某些流支持缓存功能,该方法将把缓存中所有内容强制输出到流中。
  49. */
  50. public void flush() throws IOException {
  51. }
  52. /**
  53. * 关闭当前流、释放所有与当前流有关的资源
  54. */
  55. public void close() throws IOException {
  56. }
  57. }

总结

1、OutputStream,字节出流,用于将内容通过流的方式从应用程序输出到外部设备,此抽象类是**表示字节输出流的所有类的超类**。为字节输出流提供一个标准和基本的方法及简单的实现,子类可以根据自己的特点进行重写和扩展。

2、OutputStream中有一个抽象方法write(),是字节输出流的核心方法,要求子类必须实现此方法