Java中的FileInputStream與FileOutputStream的基本使用詳解__java

來源:互聯網
上載者:User
什麼是InputStream和OutputStream。

InputStream和OutputStream是抽象類別,是所有位元組輸入資料流和輸出資料流的父類。這裡,我們首先要分清楚兩個概念: InputStream(輸入資料流):輸入資料流是用來讀入資料的。- - - > > >讀入 OutputStream(輸出資料流):輸出資料流是用來寫出資料的。- - - > > >寫出 檔案輸入資料流——FileInputStream

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

//通過開啟一個到實際檔案的串連來建立一個 FileInputStream,該檔案通過檔案系統中的 File 對象 file 指定。public FileInputStream(File file);//通過開啟一個到實際檔案的串連來建立一個 FileInputStream,該檔案通過檔案系統中的路徑名 name 指定。public FileInputStream(String name);
常用的方法 從輸入資料流中讀取一個位元組大小的資料
//從此輸入資料流中讀取一個資料位元組。public int read();
從輸入資料流一次讀取一個位元組數組
//從此輸入資料流中將最多 b.length 個位元組的資料讀入一個 byte 數組中。public int read(byte[] b);//從此輸入資料流中將最多 len 個位元組的資料讀入一個 byte 數組中。off:目標數組 b 中的起始位移量。public int read(byte[] b,int off,int len);
從檔案中讀取資料:
import java.io.FileInputStream;/** * FileInputStream:節點流(低級流),從檔案中讀入資料 * @author Administrator * */public class FISDemo01 {    public static void main(String[] args){        String content=null;        try {            int size=0;            //定義一個位元組緩衝區,該緩衝區的大小根據需要來定義            byte[] buffer=new byte[1024];            FileInputStream fis=new FileInputStream("FOSDemo.txt");            //迴圈來讀取該檔案中的資料            while((size=fis.read(buffer))!=-1){                content=new String(buffer, 0, size);                System.out.println(content);            }        //關閉此檔案輸入資料流並釋放與此流有關的所有系統資源。         fis.close();        } catch (Exception e) {            e.printStackTrace();        }    }}
檔案輸出資料流——FileOutputStream

檔案輸出資料流是用於將資料寫入到檔案中。 構造方法

//建立一個向指定 File 對象表示的檔案中寫入資料的檔案輸出資料流。public FileOutputStream(File file);//建立一個向指定 File 對象表示的檔案中寫入資料的檔案輸出資料流。如果第二個參數為 true,則將位元組寫入檔案末尾處,而不是寫入檔案開始處。public FileOutputStream(File file,boolean append);//建立一個向具有指定名稱的檔案中寫入資料的輸出檔案流。public FileOutputStream(String name);//建立一個向具有指定 name 的檔案中寫入資料的輸出檔案流。如果第二個參數為 true,則將位元組寫入檔案末尾處,而不是寫入檔案開始處。public FileOutputStream(String name,boolean append);
常用的方法 向檔案中寫入一個位元組大小的資料
//向檔案中寫入一個位元組大小的資料public void write(int b);
向檔案中一次性寫入一個位元組數組的資料
//將 b.length 個位元組從指定 byte 數組寫入此檔案輸出資料流中。public void write(byte[] b);//指定 byte 數組中從位移量 off 開始的 len 個位元組寫入此檔案輸出資料流。 public void write(byte[] b,int off,int len);
向檔案中寫出資料:
import java.io.FileOutputStream;/** * FileOutputStream:節點流(低級流),向檔案中寫出資料  * @author Administrator * */public class FOSDemo01 {    public static void main(String[] args){        try {            //向檔案中寫入位元組數組            String font="輸出資料流是用來寫入資料的。";            FileOutputStream fos = new FileOutputStream("FOSDemo.txt");            fos.write(font.getBytes());            //關閉此檔案輸出資料流並釋放與此流有關的所有系統資源。此檔案輸出資料流不能再用於寫入位元組。 如果此流有一個與之關聯的通道,則關閉該通道。             fos.close();        } catch (Exception e) {            e.printStackTrace();        }    }}
使用FileInputStream和FileOutputStream實現檔案的複製
import java.io.FileInputStream;import java.io.FileOutputStream;/** * 使用檔案輸入資料流和檔案輸出資料流實現檔案的複製 * @author Administrator * */public class SummaryFISAndFOS {    public static void main(String[] args){        /**         * 1.先將檔案中的內容讀入到輸入資料流中         * 2.將輸入資料流中的資料通過輸出資料流寫入到目標檔案中         * 3.關閉輸入資料流和輸出資料流         */        try {            long begin=System.currentTimeMillis();            //從輸入資料流中讀取資料            FileInputStream fis=new FileInputStream("FOSDemo.txt");            //向輸出資料流中寫入資料            FileOutputStream fos=new FileOutputStream("FISAndFOSDest.txt");            //先定義一個位元組緩衝區,減少I/O次數,提高讀寫效率            byte[] buffer=new byte[10240];            int size=0;            while((size=fis.read(buffer))!=-1){                fos.write(buffer, 0, size);            }            fis.close();            fos.close();            long end=System.currentTimeMillis();            System.out.println("使用檔案輸入資料流和檔案輸出資料流實現檔案的複製完畢。耗時:"+(end-begin)+"毫秒");        } catch (Exception e) {            e.printStackTrace();        }        //解決JNI問題(Java Native Interface)        System.exit(0);    }}

運行結果:
使用檔案輸入資料流和檔案輸出資料流實現檔案的複製完畢。耗時:17毫秒

以上基本上就是我們經常用到的方法的介紹,記得最後一定要close()哦。 以上內容只代表我個人的觀點,有什麼錯誤的地方請各路大神指正。轉載請註明出處。謝謝。

聯繫我們

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