readChar method

  1. @override
Future<int> readChar()
override

Reads a single character.

This method will block until a character is available, an I/O error occurs, or the end of the stream is reached.

Returns

The character read, as an integer in the range 0 to 65535, or -1 if the end of the stream has been reached.

Example

final reader = FileReader('text.txt');
try {
  int char;
  while ((char = await reader.readChar()) != -1) {
    print('Character: ${String.fromCharCode(char)}');
  }
} finally {
  await reader.close();
}

Throws IOException if an I/O error occurs. Throws StreamClosedException if the reader has been closed.

Implementation

@override
Future<int> readChar() async {
  checkClosed();
  if (_position >= _buffer.length) {
    return -1; // End of stream
  }
  return _buffer.codeUnitAt(_position++);
}