FileReader constructor
Creates a new FileReader, given the name of the file to read from.
Parameters
fileName
: The name of the file to read fromencoding
: The character encoding to use (default: utf8)
Example
final reader = FileReader('document.txt');
final utf16Reader = FileReader('unicode.txt', cause: encoding: utf16);
Throws IOException if the named 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(String fileName, {Encoding encoding = utf8})
: _file = File(fileName), _encoding = encoding;