四、字节流

一、字节流
1.概述:

1、字节流和字符流的基本操作是相同的,但是要想操作媒体流就需要用到字节流。
2、字节流因为操作的是字节,所以可以用来操作媒体文件。(媒体文件也是以字节存储的)
3、读写字节流:InputStream 输入流(读)和OutputStream 输出流(写)
4、字节流操作可以不用刷新流操作。
5、InputStream特有方法:
int available();//返回文件中的字节个数
注:可以利用此方法来指定读取方式中传入数组的长度,从而省去循环判断。但是如果文件较大,而虚拟机启动分配的默认内存一般为64M。当文件过大时,此数组长度所占内存空间就会溢出。所以,此方法慎用,当文件不大时,可以使用。
练习:
需求:复制一张图片F:\java_Demo\day9_28\1.BMP到F:\java_Demo\day9_28\2.bmp

[java] view plain copy

  1. import java.io.*;

  2. class CopyPic {

  3.  public static void main(String[] args){  
    
  4.      copyBmp();  
    
  5.      System.out.println("复制完成");  
    
  6.  }  
    
  7. public static void copyBmp() {  
    
  8.     FileInputStream fis = null;  
    
  9.     FileOutputStream fos = null;  
    
  10.     try {  
    
  11.         fis = new FileInputStream("F:\\java_Demo\\day9_28\\1.bmp");             //写入流关联文件  
    
  12.         fos = new FileOutputStream("F:\\java_Demo\\day9_28\\2.bmp");            //读取流关联文件  
    
  13.         byte[] copy = new byte[1024];  
    
  14.         int len = 0;  
    
  15.         while((len=fis.read(copy))!=-1) {  
    
  16.         fos.write(copy,0,len);  
    
  17.         }  
    
  18.     }  
    
  19.     catch (IOException e) {  
    
  20.         e.printStackTrace();  
    
  21.         throw new RuntimeException("复制文件异常");  
    
  22.     }  
    
  23.     finally {  
    
  24.         try {  
    
  25.             if(fis!=null) fis.close();  
    
  26.         }  
    
  27.         catch (IOException e) {  
    
  28.             e.printStackTrace();  
    
  29.             throw new RuntimeException("读取流");  
    
  30.         }  
    
  31.     }  
    
  32. }  
    
  33. }

  34. 字节流缓冲区

  • 字节流缓冲区跟字符流缓冲区一样,也是为了提高效率。
    注意事项:
  1. read():会将字节byte()提升为int型值
  2. write():会将int类型转换为byte()类型,保留最后的8位。
    练习:
    1.复制MP3文件 1.MP3 --> 2.MP3
    2.自己写一个MyBufferedInputStream缓冲类,提升复制速度
    代码:

[java] view plain copy

  1. import java.io.*;
  2. //自己的BufferedInputStream
  3. class MyBufferedInputStream {
  4.  private InputStream in;                         //定义一个流对象  
    
  5.  private byte [] buf = new byte[1024*4];  
    
  6.  private int count = 0,pos = 0;  
    
  7.  public MyBufferedInputStream(InputStream in){  
    
  8.     this.in = in;  
    
  9. }  
    
  10. public  int MyRead() throws IOException{  
    
  11.     if(count==0) {              //当数组里的数据为空时候,读入数据  
    
  12.         count = in.read(buf);  
    
  13.         pos = 0;  
    
  14.         byte b = buf[pos];  
    
  15.         count--;  
    
  16.         pos++;  
    
  17.         return b&255;       //提升为int类型,在前面三个字节补充0。避免1111 1111 1111 1111  
    
  18.     }  
    
  19.     else if(count > 0) {  
    
  20.         byte b = buf[pos];  
    
  21.         pos++;  
    
  22.         count--;  
    
  23.         return b&0xff;      //提升为int类型,在前面三个字节补充0。避免1111 1111 1111 1111  
    
  24.     }  
    
  25.     return -1;  
    
  26. }  
    
  27. public void myClose() throws IOException{  
    
  28.     in.close();  
    
  29. }  
    
  30. }
  31. class BufferedCopyDemo {
  32. public static void main(String[] args) {  
    
  33.     long start = System.currentTimeMillis();  
    
  34.     copy();  
    
  35.     long end = System.currentTimeMillis();  
    
  36.     System.out.println("时间:"+(end-start)+"ms");  
    
  37.     start = System.currentTimeMillis();  
    
  38.     copy1();  
    
  39.     end = System.currentTimeMillis();  
    
  40.     System.out.println("时间:"+(end-start)+"ms");  
    
  41. }   
    
  42. public static void copy1() { // 应用自己的缓冲区缓冲数据
  43.     MyBufferedInputStream bis = null;  
    
  44.     BufferedOutputStream  bos = null;  
    
  45.     try {  
    
  46.         bis = new MyBufferedInputStream(new FileInputStream("马旭东-入戏太深.mp3"));//匿名类,传入一个InputStream流对象  
    
  47.         bos = new BufferedOutputStream(new FileOutputStream("3.mp3"));  
    
  48.         int buf = 0;  
    
  49.         while((buf=bis.MyRead())!=-1) {  
    
  50.             bos.write(buf);  
    
  51.         }  
    
  52.     }  
    
  53.     catch (IOException e) {  
    
  54.         e.printStackTrace();  
    
  55.         throw new RuntimeException("复制失败");  
    
  56.     }  
    
  57.     finally {  
    
  58.         try {  
    
  59.             if(bis!=null)  {  
    
  60.                 bis.myClose();  
    
  61.                 bos.close();  
    
  62.             }  
    
  63.         }  
    
  64.         catch (IOException e) {  
    
  65.             e.printStackTrace();  
    
  66.         }  
    
  67.     }  
    
  68. }  
    
  69. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值