字節緩衝流--複製文件

public class BufferTestMain {
        public static void main(String[] args) throws IOException {
                FileInputStream fis = new FileInputStream("D:\\1upload\\121.rar");  
                BufferedInputStream bfis = new BufferedInputStream(fis);
                FileOutputStream fos = new FileOutputStream("kaobei.mp4");
                BufferedOutputStream bfos = new BufferedOutputStream(fos);

                // 方法一:
               int b = 0;
                while ((b = bfis.read()) != -1) { 
                 //看上去是一個字節一個字節的讀,其實系統實現是一次讀 8192 個字節到緩衝區
                        bfos.write(b);
                }
                // 方法二:更快,緩衝區自帶一個8192緩衝區,自己還定義了一個1024的緩衝區       
                byte[] bytes = new byte[1024];
                int len = 0;
                while ((len = bfis.read(bytes)) != -1) {
                        bfos.write(bytes, 0, len);
                }
        }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章