Java - ByteArrayInputStream close() 方法



描述

Java ByteArrayInputStream close() 方法釋放與流關聯的任何系統資源。在呼叫 close() 方法之後,可以呼叫此類中的方法,而不會產生 I/O 錯誤。

宣告

以下是 java.io.ByteArrayInputStream.close() 方法的宣告:

public void close()

引數

返回值

此方法不返回任何值。

異常

IOException - 如果發生 I/O 錯誤

示例 1

以下示例演示了 Java ByteArrayInputStream close() 方法的用法。我們建立了一個名為 buf 的 byte[] 變數並初始化了一些位元組。我們建立了一個 ByteArrayInputStream 引用,然後用 buf 變數初始化它。我們在呼叫 read() 和 available() 方法之前使用了 close() 方法來關閉流。然後在 while 迴圈中,我們使用 available() 方法檢查流是否包含任何位元組,然後使用 read() 方法讀取其位元組。

package com.tutorialspoint;
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class ByteArrayInputStreamDemo {
   public static void main(String[] args) throws IOException {
      byte[] buf = {65, 66, 67, 68, 69};
      ByteArrayInputStream bais = null;
      
      try {
         // create new byte array input stream
         bais = new ByteArrayInputStream(buf);
         
         // close invoked before read() and available() invocations
         bais.close();
         
         int count = 0;
         
         // read till the end of the stream
         while((count = bais.available())>0) {
            
            // convert byte to character
            char c = (char)bais.read();
            
            // print number of bytes available
            System.out.print("available byte(s) : "+ count);
            
            // print characters read form the byte array
            System.out.println(" & byte read : "+c);
         }
         
      } catch(Exception e) {
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         if(bais!=null)
            bais.close();
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

available byte(s) : 5 & byte read : A
available byte(s) : 4 & byte read : B
available byte(s) : 3 & byte read : C
available byte(s) : 2 & byte read : D
available byte(s) : 1 & byte read : E

示例 2

以下示例演示了 Java ByteArrayInputStream close() 方法的用法。我們建立了一個名為 buf 的 byte[] 變數並初始化了一些位元組。我們建立了一個 ByteArrayInputStream 引用,然後用 buf 變數初始化它。在 while 迴圈中,我們使用 available() 方法檢查流是否包含任何位元組,然後使用 read() 方法讀取其位元組。在 finally 塊中,我們呼叫了 close() 方法來釋放資源。

package com.tutorialspoint;
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class ByteArrayInputStreamDemo {
   public static void main(String[] args) throws IOException {
      byte[] buf = {65, 66, 67, 68, 69};
      ByteArrayInputStream bais = null;
      
      try {
         // create new byte array input stream
         bais = new ByteArrayInputStream(buf);
         
         int count = 0;
         
         // read till the end of the stream
         while((count = bais.available())>0) {
            
            // convert byte to character
            char c = (char)bais.read();
            
            // print number of bytes available
            System.out.print("available byte(s) : "+ count);
            
            // print characters read form the byte array
            System.out.println(" & byte read : "+c);
         }
         
      } catch(Exception e) {
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         if(bais!=null)
            bais.close();
      }
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

available byte(s) : 5 & byte read : A
available byte(s) : 4 & byte read : B
available byte(s) : 3 & byte read : C
available byte(s) : 2 & byte read : D
available byte(s) : 1 & byte read : E
java_bytearrayinputstream.htm
廣告
© . All rights reserved.