Java流關閉總結

來源:互聯網
上載者:User

標籤:throws   reader   cep   bsp   read   continue   也會   update   關閉   

ava中流中引用close方法總結

1.由Java.io包中的對象產生執行個體的close方法使用方式

BufferedInputStream bis = new BufferedInputStream(new InputStreamReader(new FileInputStream()))

BufferedInputStream類

public void close()    throws IOException  {    byte[] arrayOfByte;    while ((arrayOfByte = this.buf) != null)    {      if (!bufUpdater.compareAndSet(this, arrayOfByte, null))        continue;      InputStream localInputStream = this.in;      this.in = null;      if (localInputStream != null)        localInputStream.close();      return;    }

 

BufferedInputStream中的close方法中的localInputStream參數的值就是在建立BufferedInputStream執行個體時傳入的對象

 

InputStreamReader類

public void close()    throws IOException  {    this.sd.close();  }

 

InputStreamReader類中的close方法也是在建立InputStreamReader執行個體時傳入的對象。


所以,在Java中使用流的組合時,只需要關閉最外層的流就會把所有在這個最外層中會引用到的流都關閉。

以上BufferedInputStream類和InputStreamReader類的源碼皆取自在jdk中的rt.jar包中。

2.有Java.net.Socket 類擷取到的流對象
  InputStream in = new Socket("ip",port).getInputStream();
  此類流的關閉情況如下:
  1.直接關閉socket:socket.close();在關閉socket的同時也會關閉由socket建立的流
  2.in.close() 在關閉流的同時也會關閉socket通訊,

Java流關閉總結

聯繫我們

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