Java - DataInputStream read(byte[] b, int off, int len) 方法



描述

Java DataInputStream read(byte[] b, int off, int len) 方法從包含的輸入流中讀取len個位元組,並將它們分配到緩衝區b中,從b[off]開始。該方法將阻塞,直到有輸入資料可用、丟擲異常或檢測到檔案結尾。

宣告

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

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

引數

  • b - 從輸入流中讀取資料的byte[]。

  • off - b[]中的起始偏移量。

  • len - 讀取的位元組最大數量。

返回值

讀取的位元組總數,如果流已到達末尾則返回-1。

異常

  • IOException - 如果發生I/O錯誤,無法讀取第一個位元組,或在此方法之前呼叫了close()。

  • NullPointerException - 如果b為null。

  • IndexOutOfBoundsException - 如果len大於b.length - off,off為負數,或len為負數。

示例1

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

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.read(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 read(byte[] b, int offset, int len)方法的用法。我們建立了InputStream和DataInputStream引用,然後用FileInputStream和DataInputStream物件初始化它們。為了初始化DataInputStream(),我們需要FileInputStream物件。建立物件後,我們使用available()方法檢查inputStream是否包含內容。然後建立一個包含可用位元組的位元組陣列,然後將其用於DataInputStream read()方法,該方法將位元組陣列填充為無效的長度作為輸入。然後迭代並列印此位元組陣列。

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.read(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.read(DataInputStream.java:149)
	at DataInputStreamDemo.main(DataInputStreamDemo.java:25)

示例3

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

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.read(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
廣告