黑马程序员技术交流社区

标题: 自定义缓冲字节流输入 用来拷电影时出问题了,求解答 [打印本页]

作者: 客剑    时间: 2014-8-5 18:59
标题: 自定义缓冲字节流输入 用来拷电影时出问题了,求解答
本帖最后由 客剑 于 2014-8-5 19:05 编辑

自定义缓冲字节流输入 时出错,用来拷贝电影,rmvb电影只能拷63字节的内容,下午 查了2个小时没弄清楚怎么回事,哪里出错啦?!我已经疯了。
  1. /*___________________自定义缓冲输入字节流_________________________*/

  2. import java.io.*;
  3. class MyBufferedInputStream
  4. {
  5.         private InputStream in;
  6.         private byte[] buf=new byte[1024];
  7.         private int pos=0,count=0;

  8.         MyBufferedInputStream(InputStream in)
  9.         {
  10.                 this.in=in;
  11.         }

  12.         //一次读一个字节,从缓冲区(字节数组)中获取。
  13.         public int myRead()throws IOException
  14.         {
  15.                 //通过is对象读取硬盘中的数据,并存储如buf缓冲区(内存中)字节数组中。
  16.                 if(count==0)
  17.                 {
  18.                         count=in.read(buf);
  19.                         if(count<0)
  20.                                 return -1;
  21.                         pos=0;
  22.                         byte b=buf[pos];

  23.                         count--;
  24.                         pos++;
  25.                         return b;
  26.                 }
  27.                 else if(count>0)
  28.                 {
  29.                         byte b=buf[pos];

  30.                         count--;
  31.                         pos++;
  32.                         return b;
  33.                 }
  34.                 else
  35.                         return -1;
  36.         }
  37.         public void myClose()throws IOException
  38.         {
  39.                 in.close();
  40.         }
  41. }
  42. class T
  43. {
  44.         public static void main(String[] args)throws IOException
  45.         {
  46.                 long start=System.currentTimeMillis();

  47.                 //文件并联
  48.                 FileInputStream fis=new FileInputStream("I:\\欧美电影\\【电影www.quanji.com】蒙羞之旅bd中英双字1280高清.rmvb");
  49.                 FileOutputStream fos=new FileOutputStream("H:\\java201406\\19\\蒙羞之旅.rmvb");
  50.                 MyBufferedInputStream mbls=new MyBufferedInputStream(fis);
  51.                 BufferedOutputStream bfos=new BufferedOutputStream(fos);
  52.                 int bt=0;
  53.                 while((bt=mbls.myRead())!=-1)
  54.                 {
  55.                         bfos.write(bt);                        
  56.                 }

  57.                 //关闭资源               
  58.                 mbls.myClose();
  59.                 bfos.close();

  60.                 long end=System.currentTimeMillis();
  61.                 System.out.println((end-start)+"毫秒");
  62.         }
  63. }
复制代码




作者: supertoy    时间: 2014-8-5 19:06
return b&0xff
作者: 客剑    时间: 2014-8-5 19:07
supertoy 发表于 2014-8-5 19:06
return b&0xff

老毕视频上也没有加的呀
作者: supertoy    时间: 2014-8-5 19:12
黑马程序员_毕向东_Java基础视频教程第19天-14-IO流(自定义字节流的缓冲区-read和write的特点).avi
作者: supertoy    时间: 2014-8-5 19:13
在视频的20分左右
作者: 客剑    时间: 2014-8-5 20:43
supertoy 发表于 2014-8-5 19:13
在视频的20分左右

加上去确实好了,可是老毕的视频上没加也能运行咋回事啊
作者: 客剑    时间: 2014-8-5 21:22
YOLO 发表于 2014-8-5 19:09
是啊,老毕的没有啊,这不过呀!

可能他的.mp3文件中就没遇到-1这个字节,不然也得读取出错




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2