Java - DataInputStream readByte() 方法



描述

Java DataInputStream readByte() 方法讀取並返回單個輸入位元組。該位元組是範圍在 -128 到 127 之間的有符號值。

宣告

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

public final byte readByte()

引數

返回值

讀取的位元組值。

異常

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

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

示例 1

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

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);
         
         // readByte till the data available to read
         while( dis.available() >0) {
            System.out.println(dis.readByte());
         }
         
      } 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();
      }   
   }
}

輸出

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

65
0
0
68
69

示例 2

以下示例演示了 Java DataInputStream readByte() 方法的用法。我們建立了 InputStream 和 DataInputStream 引用,然後使用 ByteArrayInputStream(使用位元組陣列填充)和 DataInputStream 物件初始化它們。為了初始化 DataInputStream(),我們需要 ByteArrayInputStream 物件。建立物件後,我們將使用 available() 方法檢查 DataInputStream 是否有內容。現在,作為一個特例,我們將關閉流,然後使用 readByte() 方法讀取位元組,看看此方法是否丟擲異常。結果,我們可以看到 readByte() 忽略 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();
         
         // readByte till the data available to read
         while( dis.available() >0) {
            System.out.println(dis.readByte());
         }
         
      } 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();
      }   
   }
}

輸出

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

65
0
0
68
69

示例 3

以下示例演示了 Java DataInputStream readByte() 方法的用法。我們建立了 InputStream 和 DataInputStream 引用,然後使用 ByteArrayInputStream(使用位元組陣列填充)和 DataInputStream 物件初始化它們。為了初始化 DataInputStream(),我們需要 ByteArrayInputStream 物件。建立物件後,我們將使用 available() 方法檢查 DataInputStream 是否有內容。然後使用 readByte() 方法讀取位元組並將流指標移動到下一個位元組。現在,作為一個特例,我們將使用 readByte() 方法在讀取所有位元組後讀取位元組。結果,我們可以看到 readByte() 丟擲 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);
         
         // readByte till the data available to read
         while( dis.available() >0) {
            System.out.println(dis.readByte());
         }
         System.out.println(dis.readByte());
      } 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();
      }   
   }
}

輸出

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

65
0
0
68
69
java.io.EOFException
	at java.base/java.io.DataInputStream.readByte(DataInputStream.java:272)
	at DataInputStreamDemo.main(DataInputStreamDemo.java:23)
java_files_io.htm
廣告