IO流-字节流

字节流

IO流概述和分类

  • 分类
    • 按照数据的流向
      • 输入流:读数据
      • 输出流:写数据
    • 按照数据类型来分 (默认)
      • 字节流
        • 字节输入流;字节输出流
        • 字符输入流;字符输出流
1.字节输入流
  • InputStream 父类
    • FileInputStream

      • 构造方法:

        1. FileInputStream file = new FileInputStream(“文件路径”);
        2. FileInputStream file = new FileInputStream​(File file);
      • 获取方法:

        方法名描述
        close​()关闭此输入流并释放与流相关联的任何系统资源。
        read​()一次读取一个字节数据
        read​(byte[] b)从该输入流读取最多 b.length个字节的数据到一个字节数组。
        read​(byte[] b, int off, int len)一次读取一个字节数组指定长度数据
        方式一
        FileInputStream fis = new FileInputStream("文件路径")int len=fis.read() 一次读取一个字节 len代表读取到的一个字节
        
        方式二
        一次读取一个字节数组
        byte[] arr =new byte[1024]
        int len=fis.read(arr);	len代表读取到的字节数组里面的个数			
        
    • BufferedInputStream

      • 构造方法:
        BufferedInputStream br = new BufferedInputStream(new FileInputStream(“文件路径”));

      • 获取方法:

        方法名描述
        close​()关闭此输入流并释放与流相关联的任何系统资源。
        read​()一次读取一个字节
        read​(byte[] b, int off, int len)一次读取一个字节数组
        方式一
        BufferedInputStream bis = new  BufferedInputStream(new FileInputStream("文件路径") )int len=bis.read() 一次读取一个字节 len代表读取到的一个字节
        
        方式二
        一次读取一个字节数组
        byte[] arr =new byte[1024]
        int len=br.read(arr);	len代表读取到的字节数组里面的个数
        br.read(arr)
        
2.字节输出流
  • OutputStream 父类
    • FileOutputStream

      • 构造方法:

        1. FileOutputStream file = FileOutputStream(“文件路径”);
        2. FileOutputStream file = FileOutputStream(File file);
        3. FileOutputStream​(String name, boolean append) 在文件后可追加内容
      • 获取方法:

        方法名描述
        close​()关闭此输入流并释放与流相关联的任何系统资源。
        write(int b)一次写入一个字节
        write(byte[] b, int off, int len)一次写入一个字节数组的一部分或全部
        方式一 
        FileOutputStream fos = new  FileOutputStream("文件路径");
        fos.write()
        
        方式二 
        一次写一个字节数组
        byte[] arr =new byte[1024]
        int len=br.read(arr);	len代表读取到的字节数组里面的个数
        fos.write(char[],0,len)
        
    • BufferedOutputStream

      • BufferedOutputStream br = new BufferedOutputStream(new FileOutputStream(“文件路径”));

      • 获取方法:

        方法名描述
        flush​()刷新缓冲输出流。
        write(int b)一次写入一个字节
        write(byte[] b, int off, int len)将指定的字节写入缓冲的输出流。

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值