diff --git a/dart/file_io.dart b/dart/file_io.dart new file mode 100644 index 0000000..ebe5474 --- /dev/null +++ b/dart/file_io.dart @@ -0,0 +1,25 @@ +import 'dart:io'; + +void main() async { + try { + File file = File('sample.txt'); + if (await file.exists()) { + List lines = await file.readAsLines(); + int wordCount = countWords(lines); + print("Word count: $wordCount"); + } else { + print("File not found."); + } + } catch (e) { + print("An error occurred: $e"); + } +} + +int countWords(List lines) { + int count = 0; + for (String line in lines) { + List words = line.split(' '); + count += words.length; + } + return count; +}