Java の FileInputStream クラスは、ファイルからバイト形式でデータを読み取るために使用されます。画像や音声ファイルなどのバイナリデータの読み取りに最適です。テキストファイルを読むには、使用することをお勧めします ファイルリーダー。
- 直接アクセス: バッファリングせずにディスクからファイルの内容を直接読み取ります。
- プラットフォームに依存しない: どのオペレーティング システムでも動作します
宣言
FileInputStream クラスは、 入力ストリーム これは、ファイルから生のバイトデータを読み取るためのメソッドを継承することを意味します。
パブリッククラスFileInputStreamはInputStreamを拡張します
例: ファイルからデータを読み取る FileInputStream クラス。
Javaimport java.io.*; public class Geeks{ public static void main(String[] args){ // Use try-with-resources to automatically close the // stream try (FileInputStream fi = new FileInputStream('file1.txt')) { // Display file channel information System.out.println('Channel: ' + fi.getChannel()); // Display file descriptor System.out.println('File Descriptor: ' + fi.getFD()); // Show available bytes in the stream System.out.println('Number of remaining bytes: ' + fi.available()); // Skip first few bytes fi.skip(4); System.out.println('File Contents:'); // Read and print file content int ch; while ((ch = fi.read()) != -1) { System.out.print((char)ch); } } catch (FileNotFoundException e) { System.out.println( 'File not found: Ensure 'file1.txt' exists in the working directory.'); } catch (IOException e) { System.out.println( 'An error occurred while reading the file: ' + e.getMessage()); } } }
出力:
出力のコンストラクター FileInputStream クラス
1. FileInputStream(文字列名)
指定された名前のファイルから読み取る入力ファイル ストリームを作成します。
FileInputStream fi = new FileInputStream('example.txt');
2. FileInputStream(ファイルファイル)
指定された File オブジェクトから読み取る入力ファイル ストリームを作成します。
ファイル f = 新しいファイル('example.txt');
FileInputStream fi = 新しい FileInputStream(f);
3. FileInputStream(FileDescriptor fdobj)
指定されたファイル記述子から読み取る入力ファイル ストリームを作成します。
ファイルディスクリプタ fd = ファイルディスクリプタ.in;
FileInputStream fi = 新しい FileInputStream(fd);
次の内容を含む file.txt という名前のファイルをプロジェクト ディレクトリに作成します。
文字列.形式
Javaこれは私の最初のコードです
これは私の2番目のコードです
import java.io.*; public class Geeks { public static void main(String[] args) { // Use try-with-resources to automatically close the stream try (FileInputStream fi = new FileInputStream('file1.txt')) { // Display file channel information System.out.println('Channel: ' + fi.getChannel()); // Display file descriptor System.out.println('File Descriptor: ' + fi.getFD()); // Illustrating available method System.out.println('Number of remaining bytes: ' + fi.available()); // Illustrating skip() method fi.skip(4); System.out.println('File Contents:'); // Reading characters from FileInputStream int ch; while ((ch = fi.read()) != -1) { System.out.print((char)ch); } } catch (FileNotFoundException e) { System.out.println( 'File not found: Ensure 'file1.txt' exists in the working directory.'); } catch (IOException e) { System.out.println( 'An error occurred while reading the file: ' + e.getMessage()); } } }
出力:
出力Javaのメソッド FileInputStream クラス
| メソッド | 実行されたアクション |
|---|---|
| 利用可能() | この入力ストリームから読み取ることができる (またはスキップできる) 残りのバイト数の推定値を返します。 |
| 近い() | このファイル入力ストリームを閉じ、ストリームに関連付けられているシステム リソースを解放します。 |
| ファイナライズ() | このファイル入力ストリームへの参照がなくなったときに、そのファイル入力ストリームの close メソッドが確実に呼び出されるようにします。 |
| getChannel() | このファイル入力ストリームに関連付けられた一意の FileChannel オブジェクトを返します。 |
| getFD() | この FileInputStream によって使用されているファイル システム内の実際のファイルへの接続を表す FileDescriptor オブジェクトを返します。 |
| 読む() | この入力ストリームからデータのバイトを読み取ります |
| 読み取り(バイト[] b) | この入力ストリームから最大 b.length バイトのデータをバイト配列に読み取ります。 |
| read(byte[] b int off int len) | この入力ストリームから最大 len バイトのデータをバイト配列に読み取ります。 |
| スキップ() | 入力ストリームから n バイトのデータをスキップして破棄します。 |