java—— 位元組流

來源:互聯網
上載者:User

標籤:exception   throw   建立   AC   資料類型   inpu   基本   inf   檔案複製   

  位元組流的繼承圖

  

  主要的位元組流有:

  InputStream/OutputStream: 這是基類,它們是抽象類別。

    FileInputStream/FileOutputStream: 輸入源和輸出目標是檔案的流。

       ByteArrayInputStream/ByteArrayOutputStream: 輸入源和輸出目標是位元組數組的流。

  DataInputStream/DataOutputStream: 裝飾類,按基本類型和字串而非只是位元組讀寫流。

  BufferedInputStream/BufferedOutputStream: 裝飾類,對輸入輸出資料流提供緩衝功能。

一、InputStream/OutputStream

  

 

     read從流中讀取下一個位元組,傳回型別為int,但取值在0到255之間,當讀到流結尾的時候,傳回值為-1

 

 

  

二、FileInputStream/FileOutputStream 檔案流  

  FileOutputStream的主要構造方法有:

    public FileOutputStream(File file) throws FileNotFoundException

    public FileOutputStream(File file, boolean append) throws FileNotFoundException

    public FileOutputStream(String name) throws FileNotFoundException

    public FileOutputStream(String name, boolean append) throws FileNotFoundException

  從構造方法上可得出:可以是File對象file,也可以是檔案路徑name。

  備忘:append參數指定是追加還是覆蓋,true表示追加,預設為 false。

  FileInputStream的主要構造方法有:

    public FileInputStream(String name) throws FileNotFoundException

    public FileInputStream(File file) throws FileNotFoundException

  示範代碼實現檔案複製

 1 public static void main(String[] args) throws IOException { 2         InputStream in = new FileInputStream("D:\\mys\\2.jpg"); 3         File copy = new File("D:\\mys\\2copy.jpg"); 4         OutputStream out = new FileOutputStream(copy); 5         byte[] b = new byte[10]; 6         int len = 0; 7         while((len = in.read(b)) != -1){ 8              out.write(b, 0, len); 9         }10         in.close();11         out.close();12     }

  建立檔案流對象會實際開啟檔案,然後作業系統會分配相關資源。如果目前使用者沒有寫入權限,會拋出異常SecurityException(父類  RuntimeException)。如果指定的檔案是一個已存在的目錄,或者由於其他原因不能開啟檔案,會拋出異常FileNotFoundException,它是IOException的一個子類。

三、ByteArrayInputStream/ByteArrayOutputStream    位元組數組流

  ByteArrayOutputStream的輸出目標是一個byte數組,這個數組的長度是根據資料內容動態擴充的。它有兩個構造方法:

    public ByteArrayOutputStream()

    public ByteArrayOutputStream(int size)

    size指定的就是初始的數組大小(預設 32)

  ByteArrayOutputStream中的資料也可以方便的寫到另一個OutputStream:

    public synchronized void writeTo(OutputStream out) throws IOException

  ByteArrayInputStream將byte數組封裝為一個輸入資料流,是一種適配器模式,它的構造方法有:

    public ByteArrayInputStream(byte buf[])

    public ByteArrayInputStream(byte buf[], int offset, int length)

    以buf中offset開始可以添加的 length 個位元組資料

四、DataInputStream/DataOutputStream    資料操作流

  允許應用程式以與機器無關方式從底層輸入資料流中讀取基本 Java 資料類型。

  DataOutputStream的構造方法:

    public DataOutputStream(OutputStream out)

  

  DataInputStream的構造方法:

    public DataInputStream(InputStream in)

  

  寫的順序與讀的順序要保持一致

  備忘:DataInputStream是裝飾類基類FilterInputStream的子類,FilterInputStream是InputStream的子類。

 五、BufferedInputStream/BufferedOutputStream   緩衝流

   緩衝流要“套接”在相應的節點流之上,對讀寫的資料提供了緩衝的功能,提高了讀寫的效率,同時增加了一些新的方法

 

     BufferedInputStream 內部有個位元組數組作為緩衝區,讀取時,先從這個緩衝區讀,緩衝區讀完了再調用封裝的流讀,它的構造方法有兩個:

      public BufferedInputStream(InputStream in)

      public BufferedInputStream(InputStream in, int size)

         size表示緩衝區大小,預設值為8192。

   BufferedOutStream 內部有個位元組數組作為緩衝區,它的構造方法也有兩個:

      public BufferedOutStream(InputStream in)

      public BufferedOutStream(InputStream in, int size)

        size表示緩衝區大小,預設值為8192。

  使用輸出資料流緩衝輸入資料流實現檔案複製

 1 public static void main(String[] args) throws IOException { 2         InputStream in = new FileInputStream("D:\\mys\\2.jpg"); 3         File copy = new File("D:\\mys\\2copy.jpg"); 4         OutputStream out = new FileOutputStream(copy); 5         BufferedInputStream bfi = new BufferedInputStream(in); 6         byte[] b = new byte[10]; 7         int len = 0; 8         while ((len = bfi.read(b)) != -1) { 9             out.write(b, 0, len);10         }11         out.close();12         //關閉緩衝流時,會自動關閉它所封裝的底層節點流13         bfi.close();14     }

  向檔案中寫出資料

1 public static void main(String[] args) throws IOException {2          FileOutputStream fos=new FileOutputStream("BOSDemo.txt");3          BufferedOutputStream bos=new BufferedOutputStream(fos);4          String content="我是緩衝輸出資料流測試資料!";5          bos.write(content.getBytes(),0,content.getBytes().length);6          //輸出的緩衝流,寫出的資料會先在記憶體中緩衝,使用flush()將會使記憶體中的資料立刻寫出7          bos.flush();8          bos.close();9     }

專門的類庫如Apache Commons IO

 

java—— 位元組流

聯繫我們

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