replaceFileContent static method

Future<void> replaceFileContent({
  1. required String filePath,
  2. required String newContent,
  3. bool createIfNotExists = false,
})

  1. Replace File Content

Implementation

static Future<void> replaceFileContent({
  required String filePath,
  required String newContent,
  bool createIfNotExists = false,
}) async {
  try {
    final file = File(filePath);
    final exists = await file.exists();

    if (!exists) {
      if (createIfNotExists) {
        await file.create(recursive: true);
      } else {
        throw Exception('❌ File not found: $filePath');
      }
    }

    await file.writeAsString(newContent);
  } catch (e) {
    rethrow;
  }
}