Java FileInputStream クラス
Java FileInputStream クラス
このチュートリアルでは、例を使用して Java FileInputStream とそのメソッドについて学習します。
FileInputStream
java.io
のクラス パッケージを使用して、ファイルからデータ (バイト単位) を読み取ることができます。
InputStream
を拡張します 抽象クラス。
FileInputStream
について学ぶ前に 、Java ファイルについて知っておく必要があります。
FileInputStream を作成する
ファイル入力ストリームを作成するには、java.io.FileInputStream
をインポートする必要があります 最初にパッケージ。パッケージをインポートしたら、Java でファイル入力ストリームを作成する方法を次に示します。
<強い>1.ファイルへのパスの使用
FileInputStream input = new FileInputStream(stringPath);
ここでは、path で指定されたファイルにリンクされる入力ストリームを作成しました。 .
<強い>2.ファイルのオブジェクトの使用
FileInputStream input = new FileInputStream(File fileObject);
ここでは、fileObject
で指定されたファイルにリンクされる入力ストリームを作成しました。 .
FileInputStream のメソッド
FileInputStream
クラスは、InputStream
に存在するさまざまなメソッドの実装を提供します クラス。
read() メソッド
read()
- ファイルから 1 バイトを読み取りますread(byte[] array)
- ファイルからバイトを読み取り、指定された配列に格納しますread(byte[] array, int start, int length)
- length に等しいバイト数を読み取ります ファイルから取得し、位置 start から始まる指定された配列に格納します
input.txt という名前のファイルがあるとします
This is a line of text inside the file.
FileInputStream
を使用してこのファイルを読み取ってみましょう .
import java.io.FileInputStream;
public class Main {
public static void main(String args[]) {
try {
FileInputStream input = new FileInputStream("input.txt");
System.out.println("Data in the file: ");
// Reads the first byte
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();
}
}
}
出力
Data in the file: This is a line of text inside the file.
上記の例では、input という名前のファイル入力ストリームを作成しました。 .入力ストリームは input.txt にリンクされています ファイル。
FileInputStream input = new FileInputStream("input.txt");
ファイルからデータを読み取るために、 read()
を使用しました while ループ内のメソッド。
available() メソッド
利用可能なバイト数を取得するには、 available()
を使用できます 方法。たとえば、
import java.io.FileInputStream;
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 input = new FileInputStream("input.txt");
// Returns the number of available bytes
System.out.println("Available bytes at the beginning: " + input.available());
// Reads 3 bytes from the file
input.read();
input.read();
input.read();
// Returns the number of available bytes
System.out.println("Available bytes at the end: " + input.available());
input.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;
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 input = new FileInputStream("input.txt");
// Skips the 5 bytes
input.skip(5);
System.out.println("Input stream after skipping 5 bytes:");
// Reads the first byte
int i = input.read();
while (i != -1) {
System.out.print((char) i);
// Reads next byte from the file
i = input.read();
}
// close() method
input.close();
}
catch (Exception e) {
e.getStackTrace();
}
}
}
出力
Input Stream after skipping 5 bytes: is a line of text inside the file.
上記の例では、skip()
を使用しています。 ファイル入力ストリームから 5 バイトのデータをスキップするメソッド。したがって、テキスト "This " を表すバイト 入力ストリームから読み取られません。
close() メソッド
ファイル入力ストリームを閉じるには、 close()
を使用できます 方法。一度 close()
メソッドが呼び出されると、入力ストリームを使用してデータを読み取ることはできません。
上記のすべての例では、close()
を使用しました。 ファイル入力ストリームを閉じるメソッド。
FileInputStream のその他のメソッド
メソッド | 説明 |
---|---|
finalize() | 確実に close() メソッドが呼び出されます |
getChannel() | FileChannel のオブジェクトを返します 入力ストリームに関連付け |
getFD() | 入力ストリームに関連付けられたファイル記述子を返します |
mark() | データが読み取られた入力ストリーム内の位置をマークします |
reset() | マークが設定された入力ストリーム内のポイントにコントロールを返します |
詳細については、Java FileInputStream (公式の Java ドキュメント) にアクセスしてください。
Java