A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 追逐 高级黑马   /  2014-3-26 09:26  /  581 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. import java.io.InputStream;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.BufferedOutputStream;

  6. class MyInputStream
  7. {
  8.         private InputStream is;
  9.         private byte[] by = new byte[1024*8];
  10.         private int pos = 0, count = 0; //pos是叫角标值,count是计数器
  11.         MyInputStream(InputStream is)
  12.         {
  13.                 this.is = is;
  14.         }

  15.         public int myRead() throws IOException
  16.         {
  17.                 if(count == 0)
  18.                 {
  19.                         count = is.read(by); //通过read方法把读取到的字节存储到byte数组中,并让count记录住数组个数(也就是数组长度)
  20.                         if(count < 0)
  21.                                 return -1; //假如元素被取完了。就返回一个-1,并停止循环
  22.                         pos = 0; //把元素存进来后要把角标值重新变成0;
  23.                         byte b = by[pos]; //取出数组中的元素
  24.                         count--;
  25.                         pos++; //元素被取出后,让计数器自减。让角标值自增
  26.                         return b & 255; //取出一个元素反出去一个元素
  27.                 }
  28.                 else if(count > 0) //如果计数器大于0.就一直读取。
  29.                 {
  30.                         byte b = by[pos]; //取出元素
  31.                         count--;
  32.                         pos++; //元素被取出后,让计数器自减。让角标值自增
  33.                         return b & 255; //元素被取出后,让计数器自减。让角标值自增
  34.                 }
  35.                 return -1;
  36.         }

  37.         public void myClose() throws IOException //关闭资源函数
  38.         {
  39.                 is.close();
  40.         }

  41. }

  42. class MyInputStreamDemo
  43. {
  44.         public static void main(String[] args)
  45.         {
  46.                 MyInputStream my = null;
  47.                 BufferedOutputStream fos = null;
  48.                 try
  49.                 {
  50.                         //关联一个文件
  51.                         my = new MyInputStream(new FileInputStream("c:\\1.mp3"));
  52.                         //定义一个写入的类
  53.                         fos = new BufferedOutputStream(new FileOutputStream("c:\\2.mp3"));
  54.                         int by = 0; //定义一个变量来记录被写入的字节
  55.                         while((by = my.myRead()) != -1)
  56.                         {
  57.                                 fos.write(by); //把字节写入新的文件中
  58.                         }
  59.                 }
  60.                 catch (IOException e)
  61.                 {
  62.                         throw new RuntimeException("读取失败"); //捕获并处理异常
  63.                 }
  64.                 finally
  65.                 {
  66.                         try
  67.                         {
  68.                                 if(my != null) //判断是否被实例化过
  69.                                         my.myClose();
  70.                         }
  71.                         catch (IOException e) //捕获并处理异常
  72.                         {
  73.                                 throw new RuntimeException("读取关闭失败");
  74.                         }
  75.                         finally
  76.                         {
  77.                                 try
  78.                                 {
  79.                                         if(fos != null) //判断是否被实例化过
  80.                                                 fos.close();
  81.                                 }
  82.                                 catch (IOException e) //捕获并处理异常
  83.                                 {
  84.                                         throw new RuntimeException("写入关闭失败");
  85.                                 }
  86.                         }
  87.                 }
  88.         }
  89. }
复制代码


评分

参与人数 1技术分 +1 收起 理由
枫儿 + 1 赞一个!

查看全部评分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马