FileInputStream.fromFile constructor

FileInputStream.fromFile(
  1. File file
)

Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system.

Parameters

  • file: The file to be opened for reading

Example

final file = File('data.bin');
final input = FileInputStream.fromFile(file);
try {
  final data = await input.readAll();
  processData(data);
} finally {
  await input.close();
}

Throws IOException if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.

A FileInputStream obtains input bytes from a file in a file system.

FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.

Example Usage

// Reading a binary file
final input = FileInputStream('image.png');
try {
  final header = await input.readFully(8); // Read PNG header
  if (isPngHeader(header)) {
    final imageData = await input.readAll();
    processImage(imageData);
  }
} finally {
  await input.close();
}

// Reading with buffer
final input = FileInputStream('data.bin');
try {
  final buffer = Uint8List(1024);
  int bytesRead;
  while ((bytesRead = await input.read(buffer)) != -1) {
    processChunk(buffer.sublist(0, bytesRead));
  }
} finally {
  await input.close();
}

Implementation

FileInputStream.fromFile(File file) : _file = file;