Java SE學習_FileInputStream和FileOutputStream

來源:互聯網
上載者:User

    FileInputStream和FileOutputStream

FileInputStream :

     用於順序訪問本地檔案,用於讀取諸像資料之類的原始位元組流,從超類InputStream中繼承了read,close等方法,對檔案進行操作,不支援方法和方法.它的兩個常用的構造方法是:               

              FileInputStream(String filepath/*檔案的全稱路徑*/);   

           FileInputStream(File fileObj/*描述該檔案的File對象*/);  

  

    可以用這樣的方法構造檔案輸入資料流:  

   (1)FileInputStream f1 = new FileInputStream("test.txt");
   (2)File f =new File("test.txt");

 

FileInputStream   f2 = new FileInputStream(f);
FileInputStream重寫了抽象類別 InputStream的讀取資料的方法:
public int read //從此輸入資料流中讀取一個資料位元組      

public int read(byte[ ] b);//從此輸入資料流中將最多b個位元組的資料讀入一個 byte 數組中
public int read(byte[] b,int off, int len);//從此輸入資料流中將最多len個位元組的資料讀入byte 數組中

如果讀取資料時,輸入資料流結束就返回-1.


FileOutputStream:

     FileOutputStream用於向一個文字檔寫資料.用於寫入諸像資料之類的原始位元組的流,它從超類OutputStream中繼承等方法.它常用的構造方法如下:

              FileOutputStream(String filepath/*檔案的全稱路徑*/)
              FileOutputStream(File fileobj/*描述該檔案的對象*/)

     FileOutputStream(String filepath,boolean append/*如果為真,檔案以追加方式開啟,不覆蓋已有檔案的內容,如果是假,則覆蓋原檔案的內容*/)
    FileOutputStream(File fileObj,boolean append)

    FileOutputStream的建立不依賴檔案是否存在.如果filepath表示的檔案不存在FileOutputStream在開啟前就建立它.如果檔案存在,則開啟它,準備寫入內容。如果開啟一個唯讀檔案,會引發IOException異常

聯繫我們

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