Java - DataInputStream readFully() 方法



描述

Java DataInputStream readFully(byte[] b, int off, int len) 方法從輸入流中讀取 len 個位元組。

它會阻塞,直到以下條件之一發生:

  • 有 b.length 個位元組的輸入資料可用。
  • 檢測到檔案結尾。
  • 發生任何 I/O 錯誤。

宣告

以下是 java.io.DataInputStream.readFully(byte[] b, int off, int len) 方法的宣告:

public final void readFully(byte[] b, int off, int len)

引數

  • b - 目標緩衝區。

  • off - 資料的偏移量。

  • len - 要讀取的位元組數。

返回值

此方法不返回值。

異常

  • IOException - 如果發生任何 I/O 錯誤或流已關閉。

  • EOFException - 如果此輸入流提前到達檔案結尾。

示例 1

以下示例演示了 Java DataInputStream readFully(byte[] b, int offset, int len) 方法的使用。我們建立了 InputStream 和 DataInputStream 引用,然後使用 FileInputStream 和 DataInputStream 物件對其進行了初始化。為了初始化 DataInputStream(),我們需要 FileInputStream 物件。建立物件後,我們使用 available() 方法檢查 inputStream 是否有內容。然後建立一個包含可用位元組的位元組陣列,然後將其用於 DataInputStream readFully() 方法,該方法根據給定的輸入填充位元組陣列。然後迭代並列印此位元組陣列。

import java.io.DataInputStream;
import java.io.FileInputStream;
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;
      
      try {
         // create input stream from file input stream
         is = new FileInputStream("F:\\test.txt");
         
         // create data input stream
         dis = new DataInputStream(is);
         
         // count the available bytes form the input stream
         int count = is.available();
         
         // create buffer
         byte[] bs = new byte[count];
         
         // read data into buffer
         dis.readFully(bs, 4, 3);
         
         // for each byte in the buffer
         for (byte b:bs) {

            // print the byte
            System.out.print(b+" ");
         }
         
      } 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();
      }   
   }
}

假設我們有一個文字檔案 F:/test.txt,其內容如下。此檔案將用作我們示例程式的輸入:

ABCDEFGH

輸出

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

0 0 0 0 65 66 67 0 

示例 2

以下示例演示了 Java DataInputStream readFully(byte[] b, int offset, int len) 方法的使用。我們建立了 InputStream 和 DataInputStream 引用,然後使用 FileInputStream 和 DataInputStream 物件對其進行了初始化。為了初始化 DataInputStream(),我們需要 FileInputStream 物件。建立物件後,我們使用 available() 方法檢查 inputStream 是否有內容。然後建立一個包含可用位元組的位元組陣列,然後將其用於 DataInputStream readFully() 方法,該方法使用作為輸入所需的無效長度填充位元組陣列。然後迭代並列印此位元組陣列。

import java.io.DataInputStream;
import java.io.FileInputStream;
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;
      
      try {
         // create input stream from file input stream
         is = new FileInputStream("F:\\test.txt");
         
         // create data input stream
         dis = new DataInputStream(is);
         
         // count the available bytes form the input stream
         int count = is.available();
         
         // create buffer
         byte[] bs = new byte[count];
         
         // read data into buffer with an invalid length
         dis.readFully(bs, 4, 10);
         
         // for each byte in the buffer
         for (byte b:bs) {

            // print the byte
            System.out.print(b+" ");
         }
         
      } 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();
      }   
   }
}

假設我們有一個文字檔案 F:/test.txt,其內容如下。此檔案將用作我們示例程式的輸入:

ABCDEFGH

輸出

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

java.lang.IndexOutOfBoundsException
	at java.base/java.io.FileInputStream.readBytes(Native Method)
	at java.base/java.io.FileInputStream.read(FileInputStream.java:279)
	at java.base/java.io.DataInputStream.readFully(DataInputStream.java:200)
	at DataInputStreamDemo.main(DataInputStreamDemo.java:25)

示例 3

以下示例演示了 Java DataInputStream readFully(byte[] b, int offset, int len) 方法的使用。我們建立了 InputStream 和 DataInputStream 引用,然後使用 FileInputStream 和 DataInputStream 物件對其進行了初始化。為了初始化 DataInputStream(),我們需要 FileInputStream 物件。建立物件後,我們使用 available() 方法檢查 inputStream 是否有內容。然後建立一個包含可用位元組的位元組陣列,然後將其用於 DataInputStream readFully() 方法,該方法根據給定的輸入填充位元組陣列以獲取流的所有位元組。然後迭代並列印此位元組陣列。

import java.io.DataInputStream;
import java.io.FileInputStream;
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;
      
      try {
         // create input stream from file input stream
         is = new FileInputStream("F:\\test.txt");
         
         // create data input stream
         dis = new DataInputStream(is);
         
         // count the available bytes form the input stream
         int count = is.available();
         
         // create buffer
         byte[] bs = new byte[count];
         
         // read data into buffer
         dis.readFully(bs, 0, count);
         
         // for each byte in the buffer
         for (byte b:bs) {

            // print the byte
            System.out.print(b+" ");
         }
         
      } 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();
      }   
   }
}

假設我們有一個文字檔案 F:/test.txt,其內容如下。此檔案將用作我們示例程式的輸入:

ABCDEFGH

輸出

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

65 66 67 68 69 70 71 72 
java_files_io.htm
廣告