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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© a2213502 中级黑马   /  2013-8-18 17:12  /  1235 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 a2213502 于 2013-9-3 10:12 编辑
  1. import java.io.*;


  2. class CopyMp3
  3. {
  4.         public static void main(String[] args) throws IOException
  5.         {
  6.                         copyMp3_1();
  7.         }

  8.         public static void copyMp3_1() throws IOException
  9.         {
  10.                
  11.                 MyBufferedInputStream mbis = new MyBufferedInputStream(new FileInputStream ("F:\\chengchengwang\\heimaliti\\0.mp3"));
  12.                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:\\chengchengwang\\heimaliti\\5.mp3"));
  13.                 int by=0;
  14.                 while((by=mbis.Myread())!=-1)
  15.                 {
  16.                         bos.write(by);
  17.                 }
  18.                 mbis.Myclose();
  19.                 bos.close();
  20.         }
  21.         
  22.         
  23. }
  24.         /*自定义一个缓冲区,
  25.                 首先定义一个数组
  26.                 定义一个指针
  27.                 定义一个计数器
  28.                 */
  29. class  MyBufferedInputStream
  30.         {               
  31.                 private InputStream in;
  32.                 private int count =0;
  33.                         private int pos =0;
  34.                         private byte buf [] =new byte[1024];
  35.                 MyBufferedInputStream(InputStream in)
  36.                 {
  37.                         this.in=in;
  38.                 }
  39.                 public  int  Myread()throws IOException
  40.                 {
  41.                         if(count==0)
  42.                         {
  43.                                 count=in.read(buf);
  44.                                 if(count<0)
  45.                                         return -1;
  46.                                 
  47.                                 pos=0;
  48.                                 byte b =buf[pos];
  49.                                 pos++;
  50.                                 return b;
  51.                         }
  52.                         else if (count>0)
  53.                         {
  54.                                 byte b =buf[pos];
  55.                                 count--;
  56.                                 pos++;
  57.                                 return b;
  58.                         }
  59.                         return -1;
  60.                
  61.                 }
  62.                 public  void Myclose()throws IOException
  63.                 {
  64.                         in.close();        
  65.                 }
  66.         }
复制代码

评分

参与人数 1技术分 +1 收起 理由
EYE_SEE_YOU + 1 神马都是浮云

查看全部评分

3 个回复

倒序浏览

缓冲区中无非就是封装了一个数组,并可供外界访问

本帖最后由 月黑风高 于 2013-8-28 18:21 编辑

/*
* 自定义读取缓冲区,其实就是模拟一个BufferedReader
* 分析:
* 缓冲区中无非就是封装了一个数组,并对外提供了更多的方法对数组进行访问。
* 其实这些方法操作的就是数组的角标。
*
* 原理:
* 从源中获取一批数据装进缓冲区中,在从缓冲区中不断的取出一个一个数据。
* 取完后,再继续从源中取一批数据,当源中的数据取光时,用-1作为结束标记。
*/

代码展示:
public class MyBufferedReader {
    private FileReader r;
    //针对FileReader的高效流缓冲区
    MyBufferedReader(FileReader r){
    this.r=r;
    }
       
    //定义一个数组作为缓冲区
    private char[] chs=new char[1024];
       
    //定义一个指针,便于操作数组中的元素,当操作到最后一个元素后,指针应该归0
    private int pos=0;
       
    //定义一个计数器用于记录缓冲区中的数据个数,当该数据减到0,就从源中继续获取数据到缓冲区中。
    private int count=0;
       
    public int myRead() throws IOException{
        if(count==0){
        //计数器为0,从源中获取数据到缓冲区中
        count=r.read(chs);
        //每次从源中取一批数据,指针归0
        pos=0;
    }
    //如果取出,返回值为-1,说明取完了。
    if(count<0)
    return -1;
    //每次取一个字符,指针后移,这批总的数据量减少
    char ch=chs[pos++];
    count--;
    return ch;
        }
}

评分

参与人数 1技术分 +1 收起 理由
薛鹏鹏 + 1

查看全部评分

回复 使用道具 举报
哥们,你的代码有误应该改下。import java.io.*;


class CopyMp3
{
        public static void main(String[] args) throws IOException
        {
                        copyMp3_1();
        }

        public static void copyMp3_1() throws IOException
        {

                MyBufferedInputStream mbis = new MyBufferedInputStream(new FileInputStream ("D:\\4.mp3"));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\5.mp3"));
                int by=0;
                while((by=mbis.Myread())!=-1)
                {
                        bos.write(by);
                }
                mbis.Myclose();
                bos.close();
        }


}
        /*自定义一个缓冲区,
                首先定义一个数组
                定义一个指针
                定义一个计数器
                */
class  MyBufferedInputStream
        {               
                private InputStream in;
                private int count =0;
                        private int pos =0;
                        private byte buf [] =new byte[1024];
                MyBufferedInputStream(InputStream in)
                {
                        this.in=in;
                }
                public  int  Myread()throws IOException
                {
                        if(count==0)
                        {
                                count=in.read(buf);
                                if(count<0)
                                        return -1;

                                pos=0;
                                byte b =buf[pos];
count--;//这个你忘记加了。会造成下边数组越界异常
                                pos++;
                                return b&255;//这个也要加,防止为-1
                        }
                        else if (count>0)
                        {
                                byte b =buf[pos];//前面忘记加了。会造成这里数组越界异常。因为count少减一次,会使指针多加一次
                                count--;
                                pos++;
                                return b&0xff;//这个也要加,防止为-1
                        }
                        return -1;

                }
                public  void Myclose()throws IOException
                {
                        in.close();        
                }
        }

回复 使用道具 举报
{:soso_e176:}
亲,请问下问题是否解决?
如果已解决请及时将未解决改为已解决
如果未解决请回帖追问
三天未回复的将视为已解决

详情参考: 如何更改分类

保持队形,谢谢合作{:soso_e121:}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马