This example demonstrates how to read from and write to files in Dart.
import 'dart:io';
void main() async {
// Writing to a file
var file = File('example.txt');
await file.writeAsString('Hello, Dart!\nThis is a file I/O example.\n');
print('File written: ${file.path}');
// Reading from the file
String contents = await file.readAsString();
print('File contents:\n$contents');
// Appending to the file
await file.writeAsString('Appending new content.\n', mode: FileMode.append);
contents = await file.readAsString();
print('Updated file contents:\n$contents');
}
Leave a Reply