close method

  1. @override
Future<void> close()
override

Closes this output stream and releases any system resources associated with this stream.

A closed stream cannot perform output operations and cannot be reopened. The close method of OutputStream calls flush before closing the stream.

Example

final output = FileOutputStream('output.txt');
try {
  await output.writeString('Hello, World!');
  // flush() is called automatically by close()
} finally {
  await output.close(); // Always close in finally block
}

Throws IOException if an I/O error occurs.

Implementation

@override
Future<void> close() async {
  if (!isClosed) {
    try {
      if (_randomAccessFile != null) {
        await _randomAccessFile!.flush();
        await _randomAccessFile!.close();
      }
    } catch (e) {
      throw IOException('Error closing file: ${_file.path}', cause: e);
    } finally {
      _randomAccessFile = null;
      await super.close();
    }
  }
}