位元組流複製mp3檔案(帶緩衝區)

來源:互聯網
上載者:User

標籤:style   blog   java   color   os   檔案   

//自訂的緩衝區

import java.io.*;


class  MyBufferedInputStream
{
    private byte[] buf = new byte[1024];

    private InputStream in;
   
    private int pos = 0, count = 0;

    MyBufferedInputStream(InputStream in){
        this.in = in;
    }
   
    /*
        一次讀一個位元組,從緩衝區位元組數組中讀。
    */
    public int myRead(){
        //通過in對象讀取硬碟資料,並儲存buf中
        if(count == 0){
            count = in.read(buf);
            if(count < 0)
                return -1;
            pos = 0;
            byte b = buf[pos];
            count--;
            pos++;
            return b;
        }else if(count > 0){
            byte b = buf[pos];
            count--;
            pos++;
            return b;
        }
        return -1;
    }

    public void myClose(){
        in.close();
    }
   
}

 

調用緩衝區:

import java.io.*;class CopyMp3 {    public static void main(String[] args) throws Exception     {        long start = System.currentTimeMillis();        copy_1();        long end = System.currentTimeMillis();        System.out.println(end- start);    }    //通過位元組流的緩衝區完成複製    public static void copy_2() throws Exception{        BufferedInputStream bufis = new BufferedInputStream(new FileInputStream("c:\\1.mp3"));        BufferedOutputStream bufos = new BufferedOutputStream(new FileOutputStream("c:\\2.mp3"));        int by = 0;        while((by = bufis.myRead()) != -1){            bufos.write(by);        }        bufos.close();        bufis.myClose();    }}

 

-----------------------

問題:

被複製的檔案出現0位元組

0000-0001

1111-1110

1111-1111

 

byte:-1 ---> int:-1

他會讓前面加1,為了讓前面補0,需要&255

 

最低四位&15 

1 1 1 1

最低八位&255

1111   1111

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.