Java - DataInputStream read() 方法



描述

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

宣告

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

public final int read(byte[] b)

引數

b - 從此流中讀取資料的緩衝區陣列。

返回值

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

異常

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

  • NullPointerException - 如果 b 為 null。

示例 1

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

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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);
         
         // for each byte in the buffer
         for (byte b:bs) {
         
            // convert byte into character
            char c = (char)b;
            
            // print the character
            System.out.print(c+" ");
         }
         
      } 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

輸出

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

A B C D E F G H 

示例 2

以下示例演示了 Java DataInputStream read(byte[] b) 方法的使用。我們建立了 InputStream 和 DataInputStream 引用,然後使用 FileInputStream 和 DataInputStream 物件對其進行了初始化。為了初始化 DataInputStream(),我們需要 FileInputStream 物件。在此示例中,我們使用了一個不存在的檔案,並檢視 read() 方法是否丟擲異常。

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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:\\test1.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);
         
         // for each byte in the buffer
         for (byte b:bs) {
         
            // convert byte into character
            char c = (char)b;
            
            // print the character
            System.out.print(c+" ");
         }
         
      } 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:/test1.txt。此檔案將用作我們示例程式的輸入:

輸出

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

java.io.FileNotFoundException: F:\test1.txt (The system cannot find the file specified)
	at java.base/java.io.FileInputStream.open0(Native Method)
	at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
	at java.base/java.io.FileInputStream.<init&t;(FileInputStream.java:157)
	at java.base/java.io.FileInputStream.<init&t;(FileInputStream.java:112)
	at DataInputStreamDemo.main(DataInputStreamDemo.java:13)

示例 3

以下示例演示了 Java DataInputStream read(byte[] b) 方法的使用。我們建立了 InputStream 和 DataInputStream 引用,然後使用 FileInputStream 和 DataInputStream 物件對其進行了初始化。為了初始化 DataInputStream(),我們需要 FileInputStream 物件。在此示例中,我們使用了一個空檔案,並檢視 read() 方法是否丟擲異常或能夠成功讀取空檔案。

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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);
         
         // for each byte in the buffer
         for (byte b:bs) {
         
            // convert byte into character
            char c = (char)b;
            
            // print the character
            System.out.print(c+" ");
         }
         
      } 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,其內容為空。此檔案將用作我們示例程式的輸入:


輸出

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


java_files_io.htm
廣告

© . All rights reserved.