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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© woshihuoye 中级黑马   /  2014-9-5 11:41  /  1023 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

/*
演示mp3的复制,通过缓冲区。
BufferedOutoutStream
BufferedInputStream
*/
import java.io.*;
class CopyMp3
{
        public static void main(String[] args) throws IOException
        {
                long start = System.currentTimeMillis();
                copy_2();
                long end = System.currentTimeMillis();
                System.out.println((end-start)+"毫秒");
        }
        //通过字节流的缓冲区完成复制。
        public static void copy_1()throws IOException
        {
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\0.mp3"));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\1.mp3"));
               
                int by = 0;
                while((by=bis.read())!=-1)
                {
                        bos.write(by);
                }
                bos.close();
                bis.close();
        }
        public static void copy_2()throws IOException
        {
                MyBufferedInputStream bis = new MyBufferedInputStream(new FileInputStream("C:\\0.mp3"));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("C:\\1.mp3"));
               
                int by = 0;
                //System.out.println("第一个字节:"+bis.myRead());
                while((by=bis.myRead())!=-1)
                {
                        bos.write(by);
                }
                bos.close();
                bis.myClose();
        }
}
class  MyBufferedInputStream
{
        private InputStream in;
        private byte[] buf = new byte[1024*1024];
        private        int pos = 0,count = 0;
                 
        MyBufferedInputStream(InputStream in)
        {
                this.in = in;
        }
        //一次读一个字节,从缓冲(字节数组)获取。
        public int myRead()throws IOException
        {
                //通过in对象读取硬盘上数据,并存储buf中。
                if(count==0)
                {
                        count = in.read(buf);
                        if(count<0)
                                return -1;
                        byte b = buf[pos];                       
                        pos++;
                        count--;
                        return b&255;
                }
                else if(count>0)
                {
                        byte b = buf[pos];                       
                        pos++;
                        count--;
                        return b&255;
                }
                return -1;
        }
        public void myClose()throws IOException
        {
                in.close();
        }
}
/*
11111111 -->提升了一个int类型 那还不是-1吗?是-1的原因是因为在8个1前面补的是1导致的。
那么我么只要在前面补0,即可以保留原字节数据不变,又可以避免-1的出现。只要&255即可!!
*/
****在&255前可以编译运行,但是&之后跑出ArrayOutOfBoundsException异常,详情看附件,求解问题出在哪了?

(QDWOUCIB{9L3)X69J)5O$M.jpg (51.96 KB, 下载次数: 10)

(QDWOUCIB{9L3)X69J)5O$M.jpg

4 个回复

倒序浏览
求大家给给意见
回复 使用道具 举报
顶。。。。。。。。。。。。。。。。。。。。。
回复 使用道具 举报
你的数组缓冲数组缺少归0机制。数组只有1024*1024的空间,而下标pos一直增加,从不归0,只要文件大小超过1MB,数组下标就越界了
回复 使用道具 举报
fantacyleo 发表于 2014-9-5 13:08
你的数组缓冲数组缺少归0机制。数组只有1024*1024的空间,而下标pos一直增加,从不归0,只要文件大小超过1M ...

谢谢,大神,哈哈,还是吧友给力,我的问题解决了,给个赞!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马