1. 程式人生 > >java io詳解三:位元組輸入輸出流

java io詳解三:位元組輸入輸出流

這篇部落格我們講的是位元組輸入輸出流:InputStream、OutputSteam(下圖紅色長方形框內),紅色橢圓框內是其典型實現(FileInputSteam、FileOutStream)

  

 

 1、位元組輸出流:OutputStream

1

2

3

public abstract class OutputStream

      extends Object

      implements Closeable, Flushable

  這個抽象類是表示位元組輸出流的所有類的超類。 輸出流接收輸出位元組並將其傳送到某個接收器。

  方法摘要:

  

 

  下面我們用 位元組輸出流 OutputStream 的典型實現 FileOutputStream 來介紹:

//1、建立目標物件,輸出流表示把資料儲存到哪個檔案。不寫碟符,預設該檔案是在該專案的根目錄下
        File target = new File("io"+File.separator+"a.txt");
        //2、建立檔案的位元組輸出流物件,第二個引數是 Boolean 型別,true 表示後面寫入的檔案追加到資料後面,false 表示覆蓋
        OutputStream out = new FileOutputStream(target,true);
        //3、具體的 IO 操作(將資料寫入到檔案 a.txt 中)
            /**
             * void write(int b):把一個位元組寫入到檔案中
             * void write(byte[] b):把陣列b 中的所有位元組寫入到檔案中
             * void write(byte[] b,int off,int len):把陣列b 中的從 off 索引開始的 len 個位元組寫入到檔案中
             */
        out.write(65); //將 A 寫入到檔案中
        out.write("Aa".getBytes()); //將 Aa 寫入到檔案中
        out.write("ABCDEFG".getBytes(), 1, 5); //將 BCDEF 寫入到檔案中
        //經過上面的操作,a.txt 檔案中資料為 AAaBCDEF
         
        //4、關閉流資源
        out.close();
        System.out.println(target.getAbsolutePath());

2、位元組輸入流:InputStream

1

2

3

public abstract class InputStream

  extends Object

  implements Closeable

  這個抽象類是表示輸入位元組流的所有類的超類。

  方法摘要:

  

  下面我們用 位元組輸出流 InputStream 的典型實現 FileInputStream 來介紹:

//1、建立目標物件,輸入流表示那個檔案的資料儲存到程式中。不寫碟符,預設該檔案是在該專案的根目錄下
            //a.txt 儲存的檔案內容為:AAaBCDEF
        File target = new File("io"+File.separator+"a.txt");
        //2、建立輸入流物件
        InputStream in = new FileInputStream(target);
        //3、具體的 IO 操作(讀取 a.txt 檔案中的資料到程式中)
            /**
             * 注意:讀取檔案中的資料,讀到最後沒有資料時,返回-1
             *  int read():讀取一個位元組,返回讀取的位元組
             *  int read(byte[] b):讀取多個位元組,並儲存到陣列 b 中,從陣列 b 的索引為 0 的位置開始儲存,返回讀取了幾個位元組
             *  int read(byte[] b,int off,int len):讀取多個位元組,並存儲到陣列 b 中,從陣列b 的索引為 0 的位置開始,長度為len個位元組
             */
        //int read():讀取一個位元組,返回讀取的位元組
        int data1 = in.read();//獲取 a.txt 檔案中的資料的第一個位元組
        System.out.println((char)data1); //A
        //int read(byte[] b):讀取多個位元組儲存到陣列b 中
        byte[] buffer  = new byte[10];
        in.read(buffer);//獲取 a.txt 檔案中的前10 個位元組,並存儲到 buffer 陣列中
        System.out.println(Arrays.toString(buffer)); //[65, 97, 66, 67, 68, 69, 70, 0, 0, 0]
        System.out.println(new String(buffer)); //AaBCDEF[][][]
         
        //int read(byte[] b,int off,int len):讀取多個位元組,並存儲到陣列 b 中,從索引 off 開始到 len
        in.read(buffer, 0, 3);
        System.out.println(Arrays.toString(buffer)); //[65, 97, 66, 0, 0, 0, 0, 0, 0, 0]
        System.out.println(new String(buffer)); //AaB[][][][][][][]
        //4、關閉流資源
        in.close();

3、用位元組流完成檔案的複製

        /**
         * 將 a.txt 檔案 複製到 b.txt 中
         */
        //1、建立源和目標
        File srcFile = new File("io"+File.separator+"a.txt");
        File descFile = new File("io"+File.separator+"b.txt");
        //2、建立輸入輸出流物件
        InputStream in = new FileInputStream(srcFile);
        OutputStream out = new FileOutputStream(descFile);
        //3、讀取和寫入操作
        byte[] buffer = new byte[10];//建立一個容量為 10 的位元組陣列,儲存已經讀取的資料
        int len = -1;//表示已經讀取了多少個位元組,如果是 -1,表示已經讀取到檔案的末尾
        while((len=in.read(buffer))!=-1){
            //列印讀取的資料
            System.out.println(new String(buffer,0,len));
            //將 buffer 陣列中從 0 開始,長度為 len 的資料讀取到 b.txt 檔案中
            out.write(buffer, 0, len);
        }
        //4、關閉流資源
        out.close();
        in.close();