IOl数据流中的字节流

IOl数据流中的字节流

IO流分类
  • 根据按照数据流向 站在内存角度

输入流 读入数据
输出流 写出数据

  • 按照数据类型分为字节流和字符流

字节流 可以读写任何类型的文件 比如音频 视频 文本文件
字符流 只能读写文本文件

  • 字节流又分为字节输入流 InPutStream和字节输出流OutPutStream

​ 其中 输入流与输出流是一一对应的关系FileInPutStream —FileOutPutStrStream

​ ObjectInputStream----ObjectOutPutStream

  • 通过文件输出流来关联文件写入数据

一次读取一个字节

    FileOutputStream out = new FileOutputStream("a.txt");

​             out.write(97);

​             out.close();

一次读取一个数组

FileOutputStream out = new FileOutputStream("a.txt");

​        byte[] bytes1 = new byte[1024];

   out.write(bytes1);
        out.close();//其中的close用于释放资源,必须要加上
  • 字节流可用于文件或者文件夹的复制
     FileInputStream in = new FileInputStream("MyTest.java");
              FileOutputStream out = new FileOutputStream("E:\\MyTest.java");
    
      ​      int len=0; 
      ​        while ((len=in.read())!=-1){
      ​            out.write(len);
      ​            out.flush();//刷新
      ​        }
      ​         in.close();
      ​        out.close();
  • 高效的字节流读取 BufferedInputStream
BufferedInputStream bfr = new BufferedInputStream(new FileInputStream("demo.mp3"));
     BufferedOutputStream bfw = new BufferedOutputStream(new FileOutputStream("demo2.mp3"))

  ​      int len=0; 
  ​        while ((len=bfr.read())!=-1){
  ​            bfw.write(len);
  ​            out.flush();//刷新
  ​        }
  ​         bfr.close();
  ​        bfw.close();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值