writeChars method

Future<void> writeChars(
  1. List<int> cbuf, [
  2. int offset = 0,
  3. int? length
])

Writes an array of characters.

Parameters

  • cbuf: Array of characters to write
  • offset: Offset from which to start writing characters (default: 0)
  • length: Number of characters to write (default: remaining characters from offset)

Example

final writer = FileWriter('output.txt');
try {
  final chars = 'Hello, World!'.codeUnits;
  
  // Write entire array
  await writer.writeChars(chars);
  
  // Write part of array
  await writer.writeChars(chars, 7, 5); // Write "World"
  
  await writer.flush();
} finally {
  await writer.close();
}

Throws InvalidArgumentException if offset or length is negative, or if offset + length is greater than the length of cbuf. Throws IOException if an I/O error occurs. Throws StreamClosedException if the writer has been closed.

Implementation

Future<void> writeChars(List<int> cbuf, [int offset = 0, int? length]) async {
  checkClosed();

  length ??= cbuf.length - offset;

  if (offset < 0 || length < 0 || offset + length > cbuf.length) {
    throw InvalidArgumentException('Invalid offset or length');
  }

  for (int i = 0; i < length; i++) {
    await writeChar(cbuf[offset + i]);
  }
}