Java BufferedInputStream クラス
Java BufferedInputStream クラス
このチュートリアルでは、例を使って Java BufferedInputStream とそのメソッドについて学びます。
BufferedInputStream
java.io
のクラス package を他の入力ストリームと共に使用して、データ (バイト単位) をより効率的に読み取ります。
InputStream
を拡張します 抽象クラス。
BufferedInputStream の働き
BufferedInputStream
8192 バイトの内部バッファを維持します .
BufferedInputStream
の読み取り操作中 、バイトのチャンクがディスクから読み取られ、内部バッファーに格納されます。内部バッファからバイトが個別に読み取られます。
したがって、ディスクへの通信回数が減少します。これが、BufferedInputStream
を使用してバイトを読み取る方が速い理由です。 .
BufferedInputStream を作成する
BufferedInputStream
を作成するには 、 java.io.BufferedInputStream
をインポートする必要があります 最初にパッケージ。ここでパッケージをインポートしたら、入力ストリームを作成する方法を説明します。
// Creates a FileInputStream
FileInputStream file = new FileInputStream(String path);
// Creates a BufferedInputStream
BufferedInputStream buffer = new BufferInputStream(file);
上記の例では、BufferdInputStream
を作成しました。 名前付き バッファ FileInputStream
で 名前付き ファイル .
ここで、内部バッファのデフォルト サイズは 8192 バイトです。ただし、内部バッファーのサイズも指定できます。
// Creates a BufferedInputStream with specified size internal buffer
BufferedInputStream buffer = new BufferInputStream(file, int size);
バッファ ファイルからバイトをより速く読み取るのに役立ちます。
BufferedInputStream のメソッド
BufferedInputStream
クラスは、InputStream
に存在するさまざまなメソッドの実装を提供します クラス。
read() メソッド
read()
- 入力ストリームから 1 バイトを読み取りますread(byte[] arr)
- ストリームからバイトを読み取り、指定された配列に格納しますread(byte[] arr, int start, int length)
- length に等しいバイト数を読み取ります ストリームから取得し、位置 start から始まる指定された配列に格納します
input.txt という名前のファイルがあるとします
This is a line of text inside the file.
BufferedInputStream
を使用してファイルを読み取ってみましょう .
import java.io.BufferedInputStream;
import java.io.FileInputStream;
class Main {
public static void main(String[] args) {
try {
// Creates a FileInputStream
FileInputStream file = new FileInputStream("input.txt");
// Creates a BufferedInputStream
BufferedInputStream input = new BufferedInputStream(file);
// Reads first byte from file
int i = input .read();
while (i != -1) {
System.out.print((char) i);
// Reads next byte from the file
i = input.read();
}
input.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}
出力
This is a line of text inside the file.
上記の例では、buffer という名前のバッファリングされた入力ストリームを作成しました。 FileInputStream
とともに .入力ストリームはファイル input.txt にリンクされています .
FileInputStream file = new FileInputStream("input.txt");
BufferedInputStream buffer = new BufferedInputStream(file);
ここでは、read()
を使用しました バッファリングされたリーダーの内部バッファからバイト配列を読み取るメソッド。
available() メソッド
入力ストリームで利用可能なバイト数を取得するには、 available()
を使用できます 方法。たとえば、
import java.io.FileInputStream;
import java.io.BufferedInputStream;
public class Main {
public static void main(String args[]) {
try {
// Suppose, the input.txt file contains the following text
// This is a line of text inside the file.
FileInputStream file = new FileInputStream("input.txt");
// Creates a BufferedInputStream
BufferedInputStream buffer = new BufferedInputStream(file);
// Returns the available number of bytes
System.out.println("Available bytes at the beginning: " + buffer.available());
// Reads bytes from the file
buffer.read();
buffer.read();
buffer.read();
// Returns the available number of bytes
System.out.println("Available bytes at the end: " + buffer.available());
buffer.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}
出力
Available bytes at the beginning: 39 Available bytes at the end: 36
上記の例では、
<オール>available()
を使用します 入力ストリームで利用可能なバイト数をチェックするメソッドread()
を使用しました メソッドを 3 回呼び出して、入力ストリームから 3 バイトを読み取ります。skip() メソッド
指定されたバイト数を破棄してスキップするには、 skip()
を使用できます 方法。たとえば、
import java.io.FileInputStream;
import java.io.BufferedInputStream;
public class Main {
public static void main(String args[]) {
try {
// Suppose, the input.txt file contains the following text
// This is a line of text inside the file.
FileInputStream file = new FileInputStream("input.txt");
// Creates a BufferedInputStream
BufferedInputStream buffer = new BufferedInputStream(file);
// Skips the 5 bytes
buffer.skip(5);
System.out.println("Input stream after skipping 5 bytes:");
// Reads the first byte from input stream
int i = buffer.read();
while (i != -1) {
System.out.print((char) i);
// Reads next byte from the input stream
i = buffer.read();
}
// Closes the input stream
buffer.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}
出力
Input stream after skipping 5 bytes: is a line of text inside the file.
上記の例では、skip()
を使用しています。 ファイル入力ストリームから 5 バイトをスキップするメソッド。したがって、バイト 'T'
、 'h'
、 'i'
、 's'
と ' '
入力ストリームからスキップされます。
close() メソッド
バッファリングされた入力ストリームを閉じるには、 close()
を使用できます 方法。一度 close()
メソッドが呼び出されると、入力ストリームを使用してデータを読み取ることはできません。
BufferedInputStream のその他のメソッド
メソッド | 説明 |
---|---|
mark() | データが読み取られた入力ストリーム内の位置をマークします |
reset() | マークが設定された入力ストリーム内のポイントにコントロールを返します |
詳細については、Java BufferdInputStream (公式の Java ドキュメント) を参照してください。
Java