天天看點

IO流 InputStream輸入流、OutputStream簡單介紹

介紹

  • IO流,主要是用于處理資料的傳輸,包含位元組輸入流,位元組輸出流,字元輸入流,字元輸出流,今天着重說下位元組流;
    • 所有的位元組輸入流繼承 InputStream
      • InputStream,這個抽象類是表示輸入位元組流的所有類的超類。
      • BufferedInputStream,當建立BufferedInputStream時,将建立一個内部緩沖區數組。 當從流中讀取或跳過位元組時,内部緩沖區将根據需要從所包含的輸入流中重新填充,一次有多個位元組。 mark操作會記住輸入流中的一點,并且reset操作會導緻從最近的mark操作之後讀取的所有位元組在從包含的輸入流中取出新的位元組之前重新讀取。
      • ByteArrayInputStream,包含一個内部緩沖區,其中包含可以從流中讀取的位元組; 内部計數器跟蹤read方法要提供的下一個位元組 。
      • FileInputStream,擷取檔案輸入流。
    • 所有的位元組輸出流繼承 OutputStream
      • OutputStream,這個抽象類是表示位元組輸出流的所有類的超類。 輸出流接收輸出位元組并将其發送到某個接收器。
      • BufferedOutputStream,該類實作緩沖輸出流。 通過設定這樣的輸出流,應用程式可以向底層輸出流寫入位元組,而不必為寫入的每個位元組導緻底層系統的調用。
      • ByteArrayOutputStream,該類實作了将資料寫入位元組數組的輸出流。 當資料寫入緩沖區時,緩沖區會自動增長。
      • FileOutputStream,檔案輸出流是用于将資料寫入到檔案。

使用輸入流輸出流時注意是否需要成對出現,就好比大管道小管道似的,輸入管道要與輸出管道比對才行。

IO流使用

使用以上介紹的輸入輸出流,簡單的測試代碼

  • BufferedInputStream
public static void main(String[] args) {
        try ( InputStream inputStream =new FileInputStream("E:\\測試.txt");//讀取檔案輸入流
              BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);//聲明BufferedInputStream
              OutputStream outputStream = new FileOutputStream("E:\\測試輸出.txt");
              BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream)
                ){

            //每次讀取1024個位元組
            byte[] buffer=new byte[1024];
            int len;
            //讀到檔案末尾時傳回-1
            while ((len = bufferedInputStream.read(buffer))!=-1){
                //b - 資料。
                //off - 資料中的起始偏移量。
                //len - 要寫入的位元組數。
                bufferedOutputStream.write(buffer,0,len);
            }
            bufferedOutputStream.flush();//重新整理此輸出流,并強制将任何緩沖的輸出位元組寫入流
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
           
  • ByteArrayOutputStream

讀取固定的位元組數,效率上肯定是有一定的影響的

public static void main(String[] args) {

        try(InputStream inputStream =new FileInputStream("E:\\測試.txt");//讀取檔案輸入流
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            int len = -1;
            byte[] buffer = new byte[1024];
            while ((len = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
            System.out.println(outputStream.toString("gb2312"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
           

第一節說輸入輸出流成對的出現,接着使用StreamUtils或者Hutool工具轉換檔案輸入流為位元組數組

public static void main(String[] args) {

        try(InputStream inputStream =new FileInputStream("E:\\測試.txt");//讀取檔案輸入流
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            byte[] bytes = StreamUtils.copyToByteArray(inputStream);
            outputStream.write(bytes);
            System.out.println(outputStream.toString("gb2312"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
           

遇到的問題

使用流操作時,就一定要注意資源是否關閉,如果是手動關閉,則需要注意的是關閉的順序,遵循後打開的流先關閉的順序。

如果非正常關閉,則會造成資源的浪費,檔案的寫入錯誤,如zip檔案會出現 “不可預料的壓縮檔案末端”導緻無法打開。