Java——IO類,位元組流緩衝區

來源:互聯網
上載者:User

標籤:ade   layout   size   位元組流   檔案   資料   except   line   add   

位元組流讀取資料兩種方式比較
█ 一次讀取一個位元組 █ 一次讀取一個位元組數組        ?每次可以讀取多個資料,提高了操作效率
public class CopyDemo { //第一步:將1.txt中的內容讀入到記憶體            FileInputStream //第二步:將記憶體中讀入的資料讀入到2.txt      FileOutputStream //使用位元組流來copy東西不會出現亂碼,因為是把所有東西照搬過去,沒有拿出來解析 public static void main(String[] args) throws IOException {                 FileInputStream fis = new FileInputStream("我的滑板鞋.mp4");    //任何檔案格式都可以拷貝                 FileOutputStream fos = new FileOutputStream("kaobei.mp4"); //方法一:  效率低下,一次一個位元組 /*             int b = 0;                 while((b = fis.read()) != -1){                         fos.write(b);                 }*/ //方法二:  效率高,一次整個數組                 byte[] bytes = new byte[1024];                 int len = 0;                 while((len = fis.read(bytes)) != -1){                         fos.write(bytes,0,len);                 }                 fis.close();                 fos.close();                        } }




位元組緩衝流    ( 封裝流,加入了一些緩衝區 8092 byte )
█ 位元組流一次讀寫一個數組的速度明顯比一次讀寫一個位元組的速度快很多,這是加入了數組這樣的緩衝區效果,java本身在設計的時候,也考慮到了這樣的情況,所以提供了位元組緩衝區流 █ 位元組緩衝輸出資料流         ?BufferedOutputStream             BufferedInputStream(InputStream in);           //緩衝區大小預設是8192 建立一個 BufferedInputStream 並儲存其參數,即輸入資料流 in,以便將來使用。             BufferedInputStream(InputStream in, int size);  建立具有指定緩衝區大小的
█ 位元組緩衝輸入資料流         ?BufferedInputStream            BufferedOutputStream(OutputStream out);         建立一個新的緩衝輸出資料流,以將資料寫入指定的底層輸出資料流。            BufferedOutputStream(OutputStream out, int size);         建立一個新的緩衝輸出資料流,以將具有指定緩衝區大小的資料寫入指定的底層輸出資料流。
public class BufferTestMain {         public static void main(String[] args) throws IOException {                 FileInputStream fis = new FileInputStream("我的滑板鞋.mp4");   // 這樣的fis不帶緩衝區                 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);                 }         } }

Java——IO類,位元組流緩衝區

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.