writeBytes static method

Future<void> writeBytes(
  1. File target,
  2. List<int> content
)

Writes content to target atomically.

Writes to a .partial file in the same directory, then renames to the final path. The rename is atomic on POSIX systems and effectively atomic on Windows (best-effort).

Implementation

static Future<void> writeBytes(File target, List<int> content) async {
  final tmp = File('${target.path}.partial');
  try {
    await tmp.writeAsBytes(content, flush: true);
    tmp.renameSync(target.path);
  } finally {
    if (tmp.existsSync()) tmp.deleteSync();
  }
}