zoukankan      html  css  js  c++  java
  • java—— 字节流

      字节流的继承图

      

      主要的字节流有:

      InputStream/OutputStream: 这是基类,它们是抽象类。

        FileInputStream/FileOutputStream: 输入源和输出目标是文件的流。

           ByteArrayInputStream/ByteArrayOutputStream: 输入源和输出目标是字节数组的流。

      DataInputStream/DataOutputStream: 装饰类,按基本类型和字符串而非只是字节读写流。

      BufferedInputStream/BufferedOutputStream: 装饰类,对输入输出流提供缓冲功能。

    一、InputStream/OutputStream

      

         read从流中读取下一个字节,返回类型为int,但取值在0到255之间,当读到流结尾的时候,返回值为-1

      

    二、FileInputStream/FileOutputStream 文件流  

      FileOutputStream的主要构造方法有

        public FileOutputStream(File file) throws FileNotFoundException

        public FileOutputStream(File file, boolean append) throws FileNotFoundException

        public FileOutputStream(String name) throws FileNotFoundException

        public FileOutputStream(String name, boolean append) throws FileNotFoundException

      从构造方法上可得出:可以是File对象file,也可以是文件路径name。

      备注:append参数指定是追加还是覆盖,true表示追加,默认为 false。

      FileInputStream的主要构造方法有

        public FileInputStream(String name) throws FileNotFoundException

        public FileInputStream(File file) throws FileNotFoundException

      演示代码实现文件复制

     1 public static void main(String[] args) throws IOException {
     2         InputStream in = new FileInputStream("D:\mys\2.jpg");
     3         File copy = new File("D:\mys\2copy.jpg");
     4         OutputStream out = new FileOutputStream(copy);
     5         byte[] b = new byte[10];
     6         int len = 0;
     7         while((len = in.read(b)) != -1){
     8              out.write(b, 0, len);
     9         }
    10         in.close();
    11         out.close();
    12     }

      创建文件流对象会实际打开文件,然后操作系统会分配相关资源。如果当前用户没有写权限,会抛出异常SecurityException(父类  RuntimeException)。如果指定的文件是一个已存在的目录,或者由于其他原因不能打开文件,会抛出异常FileNotFoundException,它是IOException的一个子类。

    三、ByteArrayInputStream/ByteArrayOutputStream    字节数组流

      ByteArrayOutputStream的输出目标是一个byte数组,这个数组的长度是根据数据内容动态扩展的。它有两个构造方法:

        public ByteArrayOutputStream()

        public ByteArrayOutputStream(int size)

        size指定的就是初始的数组大小(默认 32)

      ByteArrayOutputStream中的数据也可以方便的写到另一个OutputStream:

        public synchronized void writeTo(OutputStream out) throws IOException

      ByteArrayInputStream将byte数组包装为一个输入流,是一种适配器模式,它的构造方法有:

        public ByteArrayInputStream(byte buf[])

        public ByteArrayInputStream(byte buf[], int offset, int length)

        以buf中offset开始可以添加的 length 个字节数据

    四、DataInputStream/DataOutputStream    数据操作流

      允许应用程序以与机器无关方式从底层输入流中读取基本 Java 数据类型

      DataOutputStream的构造方法:

        public DataOutputStream(OutputStream out)

      

      DataInputStream的构造方法:

        public DataInputStream(InputStream in)

      

      写的顺序与读的顺序要保持一致

      备注:DataInputStream是装饰类基类FilterInputStream的子类,FilterInputStream是InputStream的子类。

     五、BufferedInputStream/BufferedOutputStream   缓冲流

       缓冲流要“套接”在相应的节点流之上,对读写的数据提供了缓冲的功能,提高了读写的效率,同时增加了一些新的方法

         BufferedInputStream 内部有个字节数组作为缓冲区,读取时,先从这个缓冲区读,缓冲区读完了再调用包装的流读,它的构造方法有两个:

          public BufferedInputStream(InputStream in)

          public BufferedInputStream(InputStream in, int size)

             size表示缓冲区大小,默认值为8192。

       BufferedOutStream 内部有个字节数组作为缓冲区,它的构造方法也有两个:

          public BufferedOutStream(InputStream in)

          public BufferedOutStream(InputStream in, int size)

            size表示缓冲区大小,默认值为8192。

      使用输出流缓冲输入流实现文件复制

     1 public static void main(String[] args) throws IOException {
     2         InputStream in = new FileInputStream("D:\mys\2.jpg");
     3         File copy = new File("D:\mys\2copy.jpg");
     4         OutputStream out = new FileOutputStream(copy);
     5         BufferedInputStream bfi = new BufferedInputStream(in);
     6         byte[] b = new byte[10];
     7         int len = 0;
     8         while ((len = bfi.read(b)) != -1) {
     9             out.write(b, 0, len);
    10         }
    11         out.close();
    12         //关闭缓冲流时,会自动关闭它所包装的底层节点流
    13         bfi.close();
    14     }

      向文件中写出数据

    1 public static void main(String[] args) throws IOException {
    2          FileOutputStream fos=new FileOutputStream("BOSDemo.txt");
    3          BufferedOutputStream bos=new BufferedOutputStream(fos);
    4          String content="我是缓冲输出流测试数据!";
    5          bos.write(content.getBytes(),0,content.getBytes().length);
    6          //输出的缓冲流,写出的数据会先在内存中缓存,使用flush()将会使内存中的数据立刻写出
    7          bos.flush();
    8          bos.close();
    9     }

    专门的类库如Apache Commons IO

  • 相关阅读:
    副业收入是我做程序员的2倍!副业这么有 “钱”景,我要考虑转行吗?
    C语言丨const关键字的用法详解
    C/C++学习笔记:C/C++函数调用的方式,你应该要学会这五种
    软件崩溃了,该如何解决? 解决问题的关键要会对症下药!
    C语言丨深入理解volatile关键字
    C语言丨getch(),getche()和getchar()的区别
    学编程的误区——眼高手低,不重视练习!
    通过编写“猜测数字”游戏来探索Linux中的Bash
    零基础想要更快入门Linux?找对方法,让你少奋斗10年!
    VS/VC 出现闪退怎么办?这4个技巧要知道!
  • 原文地址:https://www.cnblogs.com/SacredOdysseyHD/p/8476022.html
Copyright © 2011-2022 走看看