renameContentInFile function

Future<void> renameContentInFile(
  1. String filePath,
  2. String oldText,
  3. String newText
)

Implementation

Future<void> renameContentInFile(
    String filePath, String oldText, String newText) async {
  try {
    final file = File(filePath);

    // Check if the file exists
    if (await file.exists()) {
      // Read the file contents
      String content = await file.readAsString();

      // Replace old text with new text (case-sensitive)
      content = content.replaceAll(oldText, newText);

      // Write the modified content back to the file
      await file.writeAsString(content);
      print('File content updated successfully.');
    } else {
      print('File does not exist.');
    }
  } catch (e) {
    print('Error updating file content: $e');
  }
}