Java - DataInputStream readBoolean() 方法



描述

Java DataInputStream readBoolean() 方法如果位元組非零則返回 true,如果位元組為零則返回 false。

宣告

以下是 java.io.DataInputStream.readBoolean() 方法的宣告:

public final boolean readBoolean()

引數

返回值

讀取的布林值

異常

  • IOException - 如果流已關閉且包含的輸入流在關閉後不支援讀取,或者發生其他 I/O 錯誤。

  • EOFException - 如果輸入流已到達末尾。

示例 1

以下示例演示了 Java DataInputStream readBoolean() 方法的使用。我們建立了 InputStream 和 DataInputStream 引用,然後使用 ByteArrayInputStream(使用位元組陣列填充)和 DataInputStream 物件初始化它們。為了初始化 DataInputStream(),我們需要 ByteArrayInputStream 物件。建立物件後,我們使用 available() 方法檢查 DataInputStream 是否有內容。然後使用 readBoolean() 方法,我們將位元組讀取為布林值並將流指標移動到下一個位元組。如果位元組非零,則返回 true 並列印,否則返回 false 並列印。

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
public class DataInputStreamDemo {
   public static void main(String[] args) throws IOException {
      InputStream is = null;
      DataInputStream dis = null;
      byte[] buf = {65, 0, 0, 68, 69};
      
      try {
         // create new byte array input stream
         is = new ByteArrayInputStream(buf);
         
         // create data input stream
         dis = new DataInputStream(is);
         
         // readBoolean till the data available to read
         while( dis.available() >0) {
            System.out.println(dis.readBoolean());
         }
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases any associated system files with this stream
         if(is!=null)
            is.close();
         if(dis!=null)
            dis.close();
      }   
   }
}

輸出

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

true
false
false
true
true

示例 2

以下示例演示了 Java DataInputStream readBoolean() 方法的使用。我們建立了 InputStream 和 DataInputStream 引用,然後使用 ByteArrayInputStream(使用位元組陣列填充)和 DataInputStream 物件初始化它們。為了初始化 DataInputStream(),我們需要 ByteArrayInputStream 物件。建立物件後,我們使用 available() 方法檢查 DataInputStream 是否有內容。現在,作為一個特殊情況,我們關閉流,然後使用 readBoolean() 方法將位元組讀取為布林值,以檢視此方法是否丟擲異常。結果,我們可以看到 readBoolean() 忽略了 close() 方法呼叫,因為底層輸入流在關閉後支援讀取。

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
public class DataInputStreamDemo {
   public static void main(String[] args) throws IOException {
      InputStream is = null;
      DataInputStream dis = null;
      byte[] buf = {65, 0, 0, 68, 69};
      
      try {
         // create new byte array input stream
         is = new ByteArrayInputStream(buf);
         
         // create data input stream
         dis = new DataInputStream(is);
		 
         // close the stream before reading.
		 is.close();
         dis.close();
         
         // readBoolean till the data available to read
         while( dis.available() >0) {
            System.out.println(dis.readBoolean());
         }
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases any associated system files with this stream
         if(is!=null)
            is.close();
         if(dis!=null)
            dis.close();
      }   
   }
}

輸出

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

true
false
false
true
true

示例 3

以下示例演示了 Java DataInputStream readBoolean() 方法的使用。我們建立了 InputStream 和 DataInputStream 引用,然後使用 ByteArrayInputStream(使用位元組陣列填充)和 DataInputStream 物件初始化它們。為了初始化 DataInputStream(),我們需要 ByteArrayInputStream 物件。建立物件後,我們使用 available() 方法檢查 DataInputStream 是否有內容。

然後使用 readBoolean() 方法,我們將位元組讀取為布林值並將流指標移動到下一個位元組。如果位元組非零,則返回 true 並列印,否則返回 false 並列印。現在,作為一個特殊情況,我們使用 readBoolean() 方法在讀取完所有位元組後讀取位元組。結果,我們可以看到 readBoolean() 丟擲 EOFException。

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
public class DataInputStreamDemo {
   public static void main(String[] args) throws IOException {
      InputStream is = null;
      DataInputStream dis = null;
      byte[] buf = {65, 0, 0, 68, 69};
      
      try {
         // create new byte array input stream
         is = new ByteArrayInputStream(buf);
         
         // create data input stream
         dis = new DataInputStream(is);
         
         // readBoolean till the data available to read
         while( dis.available() >0) {
            System.out.println(dis.readBoolean());
         }
         System.out.println(dis.readBoolean());
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases any associated system files with this stream
         if(is!=null)
            is.close();
         if(dis!=null)
            dis.close();
      }   
   }
}

輸出

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

true
false
false
true
true
java.io.EOFException
	at java.base/java.io.DataInputStream.readBoolean(DataInputStream.java:249)
	at DataInputStreamDemo.main(DataInputStreamDemo.java:23)
java_files_io.htm
廣告

© . All rights reserved.