【JAVA的 IO流之FileInputStream和FileOutputStream】

來源:互聯網
上載者:User

標籤:結構圖   java   

java的 IO流即輸入輸出資料流,流是一組有順序的,有起點和終點的位元組結合,是對資料轉送的總稱。即資料在兩裝置間的傳輸稱為流,流的本質是資料轉送。

    IO流可以分為位元組流和字元流。給出相應的IO結構圖:

650) this.width=650;" id="aimg_293" src="http://techfoxbbs.com/data/attachment/forum/201505/22/132147au70qijq0b0u5je0.png" class="zoom" width="570" alt="132147au70qijq0b0u5je0.png" />

在接下來的一段時間裡,將會慢慢介紹各種流的使用,本篇部落格先介紹位元組流的FileOutputStream和相對應的FileInputStream。


一.FileOutputStream(檔案輸出資料流)

     OutputStream是一個抽象類別,抽象類別必須通過子類實現。現在要向檔案裡輸出就要用FileOutputStream。


FileOutputStream有四個構造方法,分別為

1.FileOutputStream(File file)-------------向File對象的檔案寫入資料

2.FileOutputStream(File file,boolean append);------向File對象的檔案追加寫入資料

3.FileOutputStream(String path)-------------向指定檔案寫入資料

4.FileOutputStream(String path,boolean append);--------向指定檔案追加寫入資料

       當append的值為true時,向檔案中寫入的資料會追加到原資料的後面,否則會重寫該檔案的資料。預設為false。


寫入方法1:一個個位元組寫入

  1. public static void main(String[] args) {  

  2.       

  3.     try {  

  4.         //建立一個檔案位元組輸出資料流對象  

  5.         OutputStream os=new FileOutputStream("L:\\io.txt");  

  6.           

  7.         //寫入的資料  

  8.         String string="hello IO  Stream";  

  9.         byte[]bytes=string.getBytes();//轉化為位元組數組  

  10.         for(int i=0;i<bytes.length;i++){  

  11.             //向檔案輸出  

  12.             os.write(bytes[i]);  

  13.         }  

  14.         os.close();//關閉流  

  15.     } catch (FileNotFoundException e) {  

  16.         e.printStackTrace();  

  17.     } catch (IOException e) {  

  18.         e.printStackTrace();  

  19.     }  

  20.   

  21. }  

複製代碼

方法二:全部一次寫入
  1. public static void main(String[] args) {  

  2.           

  3.         try {  

  4.             //建立一個檔案位元組輸出資料流對象  

  5.             OutputStream os=new FileOutputStream("L:\\io.txt",true);//追加  

  6.               

  7.             //寫入的資料  

  8.             String string="hello IO  Stream";  

  9.             byte[]bytes=string.getBytes();//轉化為位元組數組       

  10.             os.write(bytes);//全部寫入  

  11.             //os.write(bytes,0,5)表示從0開始,寫入長度為5個位元組  

  12.             os.close();//關閉流  

  13.         } catch (FileNotFoundException e) {  

  14.             e.printStackTrace();  

  15.         } catch (IOException e) {  

  16.             e.printStackTrace();  

  17.         }  

  18.    

  19.     }  

複製代碼

一.FileInputStream(檔案輸入資料流)

   FileInputStream是從系統的某個檔案中獲得輸入位元組,有兩個構造方法

1.FileInputStream(File file)

2.FileInputStream(String path)


讀取位元組方法1:一個個位元組讀取

  1. public static void main(String[] args) {  

  2.           

  3.         try {  

  4.             //建立一個位元組輸入資料流對象  

  5.             InputStream is=new FileInputStream("L:\\io.txt");  

  6.               

  7.             int b=-1;  

  8.             while((b=is.read())!=-1)//位元組讀取,當為-1時讀取完畢  

  9.             {  

  10.                 System.out.print((char)b);//轉化為字元輸出  

  11.             }  

  12.               

  13.             is.close();//關閉流  

  14.         } catch (FileNotFoundException e) {  

  15.             e.printStackTrace();  

  16.         } catch (IOException e) {  

  17.             e.printStackTrace();  

  18.         }  

  19.    

  20.     }  

複製代碼

輸出結果為:hello IO Stream。這樣一個一個位元組讀取,速度很慢。


讀取位元組方法2:一次性讀取全部位元組

  1. public static void main(String[] args) {  

  2.           

  3.         try {  

  4.               

  5.             File file=new File("L:\\io.txt");  

  6.             //建立一個位元組輸入資料流對象  

  7.             InputStream is=new FileInputStream(file);  

  8.             //根據檔案大小來建立位元組數組  

  9.             byte[]bytes=new byte[(int)file.length()] ;  

  10.             int len=is.read(bytes);//返回讀取位元組的長度  

  11.             System.out.println("讀取位元組長度為:"+len);  

  12.               

  13.             System.out.println("讀取的內容為: "+new String(bytes));//構建成字串輸出  

  14.               

  15.             is.close();//關閉流  

  16.         } catch (FileNotFoundException e) {  

  17.             e.printStackTrace();  

  18.         } catch (IOException e) {  

  19.             e.printStackTrace();  

  20.         }  

  21.    

  22.     }  

複製代碼

運行結果: 650) this.width=650;" id="aimg_294" src="http://techfoxbbs.com/data/attachment/forum/201505/22/132250nrc1di5kpr2zi16d.png" class="zoom" width="349" alt="132250nrc1di5kpr2zi16d.png" /> 


這種讀取方法主要缺點是要構建一個和檔案大小一樣大的位元組數組,檔案小的時候還可以,當檔案很大,記憶體可能無法構架出如此大的位元組數組。所以,這種方法只適合小檔案。


讀取位元組方法3:每次讀取指定長度(最常用的方法)

  1. public static void main(String[] args) {  

  2.           

  3.         try {  

  4.   

  5.             //建立一個位元組輸入資料流對象  

  6.             InputStream is=new FileInputStream("L:\\io.txt");  

  7.             //指定每次讀取的大小--可根據效能位元組修改  

  8.             byte[]bytes=new byte[8] ;  

  9.               

  10.             StringBuffer sb=new StringBuffer();  

  11.             int len=-1;//每次讀取的實際長度  

  12.             while((len=is.read(bytes))!=-1)  

  13.             {  

  14.                 sb.append(new String(bytes,0,len));  

  15.             }  

  16.             System.out.println("讀取位元組為:"+sb);  

  17.               

  18.             is.close();//關閉流  

  19.         } catch (FileNotFoundException e) {  

  20.             e.printStackTrace();  

  21.         } catch (IOException e) {  

  22.             e.printStackTrace();  

  23.         }  

  24.    

  25.     }  

複製代碼

輸出結果:
650) this.width=650;" id="aimg_295" src="http://techfoxbbs.com/data/attachment/forum/201505/22/132320ku7uoivhohhz5h4h.png" class="zoom" width="336" alt="132320ku7uoivhohhz5h4h.png" />


【JAVA的 IO流之FileInputStream和FileOutputStream】

聯繫我們

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