FileReader.fromFile constructor

FileReader.fromFile(
  1. File file, {
  2. Encoding encoding = utf8,
})

Creates a new FileReader, given the File to read from.

Parameters

  • file: The File to read from
  • encoding: The character encoding to use (default: utf8)

Example

final file = File('document.txt');
final reader = FileReader.fromFile(file);

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.

Convenience class for reading character files.

The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an InputStreamReader on a FileInputStream.

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

Example Usage

// Reading a text file
final reader = FileReader('document.txt');
try {
  final content = await reader.readAll();
  print('Document content: $content');
} finally {
  await reader.close();
}

// Reading line by line
final reader = FileReader('data.txt');
try {
  String? line;
  int lineNumber = 1;
  while ((line = await reader.readLine()) != null) {
    print('Line $lineNumber: $line');
    lineNumber++;
  }
} finally {
  await reader.close();
}

Implementation

FileReader.fromFile(File file, {Encoding encoding = utf8})
    : _file = file, _encoding = encoding;