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.