日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java-----IO流【字节缓冲输出、输入流】

發布時間:2024/1/1 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java-----IO流【字节缓冲输出、输入流】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

字節緩沖輸出流

概述

  • 緩沖流,也叫高效流,是對4個基本都FileXxx流的增強,所以也是4個流,按照數據類型分類:

    • 字節緩沖流:BufferedInputStream,BufferedOutputStream
    • 字符緩沖流:BufferedReader,BufferedWriter

    緩沖流的基本原理,是在創建流對象時,會創建一個內置的默認大小的緩沖區數組,通過緩沖區讀寫,減少系統IO次數,從而提高讀寫的效率。

構造方法(字節緩沖流)

  • public BufferedInputStream(InputStream in):創建一個 新的緩沖輸入流
  • public BufferedOutputStream(OutputStream out):創建一個新的緩沖輸出流。

字節緩沖輸出流

  • 實例:

    public static void main(String[] args) throws Exception{// FileInputStream和BufferedInputStream都是繼承InputStream類的OutputStream out = new BufferedOutputStream(new FileOutputStream("a.txt"));out.write("Hello Worl\r\n".getBytes());// 并沒有直接寫入到文件,寫入到內存中的一個緩沖區out.write("Hello Worl\r\n".getBytes());out.flush();// 手動刷新緩沖區數據到硬盤文件// 但假如說 還沒執行到close,緩沖區已經有超過1G的文件還沒有寫入,那怎么辦呢?out.write("Hello Worlr\r\n".getBytes());// 調用close()方法的時候,會先將緩沖區中的內容刷新到硬盤的目標文件中,然后關閉流out.close();}
public static void main(String[] args) throws Exception {// 如果是這樣,就可以指定緩沖區大小將數據依次寫入。// 無論以下write多少,只要超過 1 的長度,那么就會自動寫入,自動刷新,而不是等到close完后一下寫入。OutputStream os = new BufferedOutputStream(new FileOutputStream("a.txt"),1);os.write("潛伏夫婦\r\n".getBytes());os.write("1\r\n".getBytes());os.write("余則成\r\n".getBytes());os.write("王翠萍\r\n".getBytes());os.close();} public static void main(String[] args) throws IOException {OutputStream os = new BufferedOutputStream(new FileOutputStream("a.txt"));// 如果我直接這樣輸出,絕對是輸出中國 // os.write("中國".getBytes());// 但如果我控制它的長度,如下會輸出什么?os.write("中國".getBytes(),0,2);// 第一個輸出 一個亂碼os.write("中國".getBytes(),2,4);// 第二個輸出 一個亂碼+國/** 總體執行完畢后* 還是會輸出一個”中國“* 一個中文占3個字節,因為是緩沖流,* 所以執行完close后才會輸出,其余都在緩沖內存中存放* 由此會出現字節拼接的現象* 以上共有六個字節,拼接到一塊,正好是一個"中國"* */os.close();}

字節緩沖輸入流

  • 實例:

    // 讀取文件內容到內存 public static void main(String[] args) throws IOException{InputStream in = new BufferedInputStream(new FileInputStream("a.txt"));byte[] bytes = new byte[1024];int len = 0;while((len = in.read(bytes))!=-1){System.out.println(new String(bytes,0,len));}in.close(); }

使用字節緩沖流復制文件

public static void main(String[] args) throws Exception{long l = System.currentTimeMillis();// 字節流可以輕松復制圖片和視頻文件到其他文件夾中,但是用于傳輸文本文檔內容可能會有亂碼// 創建一個讀取文件的字節輸入流InputStream is = new FileInputStream("D://b站視頻下載//【震我一下魔方宅】五階魔方基礎還原教程//mfjc.mp4");// 創建一個寫入硬盤數據的字節輸出流OutputStream os = new FileOutputStream("D://b站視頻下載//mfjc.mp4");byte[] b = new byte[1024];int len = 0;while ((len = is.read(b)) != -1){os.write(b,0,len);}os.close();is.close();long l2 = System.currentTimeMillis();System.out.println("用時:"+(l+l2)/1000+"秒");} public static void main(String[] args) throws Exception{long l1 = System.currentTimeMillis();InputStream in = new BufferedInputStream(new FileInputStream("D://b站視頻下載//【震我一下魔方宅】五階魔方基礎還原教程//mfjc.mp4"));OutputStream ou = new BufferedOutputStream(new FileOutputStream("D://b站視頻下載//mfjc.mp4"));byte[] b = new byte[1024];int len = 0;while((len = in.read(b)) != -1){ou.write(b,0,len);}ou.close();in.close();long l2 = System.currentTimeMillis();System.out.println((l1+l2)/1000);} // 用時:3233098217秒 // 3233098902// 緩沖流用時長

總結

以上是生活随笔為你收集整理的Java-----IO流【字节缓冲输出、输入流】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。