首頁 > Java > java教程 > 主體

在Java 9中何時使用InputStream的readNBytes()方法?

王林
發布: 2023-08-30 09:21:02
轉載
914 人瀏覽過

在Java 9中何时使用InputStream的readNBytes()方法?

Since Java 9, the readNBytes() method can be added to the InputStream class. This method reads the requested number of bytes from an input stream into the given byte array. This method blocks until len bytes of input data have read, end of a stream is detected, or an exception is have read, end of a stream is detected, or an exception is thrown. readNBytes() method doesn't close an input stream. This method can be useful to avoid memory #problems with large files.

Syntaxx

#problems

with large files.

Syntax

<strong>public int readNBytes(byte[] b, int off, int len) throws IOException</strong>
登入後複製

在下面的範例中,我們在來源資料夾中建立了一個名為「

Technology.txt

」的文件,其中包含簡單的資料:

{ "JAVA", "PYTHON", "JAVASCRIPT", "SELENIUM", "SCALA"}。

範例###
import java.io.*;
import java.util.stream.*;
import java.nio.*;
import java.nio.file.*;

public class InputStreamReadNByteMethodTest {
   InputStream inputStream = nputStreamReadNByteMethodTest.class.<strong>getResourceAsStream</strong>("Technology.txt");

   public void testReadNBytes() throws Exception {
      final byte[] data = new byte[10];
      inputStream.<strong>readNBytes</strong>(data, 0, 7);
      System.out.println(new String(data));
   }
   public static void main(String args[]) throws Exception {
      InputStreamReadNByteMethodTest t = new InputStreamReadNByteMethodTest();
      t.testReadNBytes();  
   }
}
登入後複製
###輸出###
<strong>"JAVA",</strong>
登入後複製
###

以上是在Java 9中何時使用InputStream的readNBytes()方法?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:tutorialspoint.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板