Open and read file line by line
This commit is contained in:
@@ -1,3 +1,6 @@
|
|||||||
|
import "dart:convert";
|
||||||
|
import "dart:io";
|
||||||
|
|
||||||
import "package:args/command_runner.dart";
|
import "package:args/command_runner.dart";
|
||||||
|
|
||||||
class Day1Command extends Command {
|
class Day1Command extends Command {
|
||||||
@@ -19,6 +22,31 @@ class Day1Command extends Command {
|
|||||||
void run() {
|
void run() {
|
||||||
// [argResults] is set before [run()] is called and contains the flags/options
|
// [argResults] is set before [run()] is called and contains the flags/options
|
||||||
// passed to this command.
|
// passed to this command.
|
||||||
print(argResults?.rest);
|
print(argResults!.rest);
|
||||||
|
|
||||||
|
if (argResults!.rest.length != 1) {
|
||||||
|
print(
|
||||||
|
"Expected 1 positional arguments, found ${argResults!.rest.length}",
|
||||||
|
);
|
||||||
|
}
|
||||||
|
var fileName = argResults!.rest[0];
|
||||||
|
|
||||||
|
var inputFile = File(fileName);
|
||||||
|
var openFile = inputFile.openRead();
|
||||||
|
|
||||||
|
openFile
|
||||||
|
.transform(utf8.decoder)
|
||||||
|
.transform(LineSplitter())
|
||||||
|
.listen(
|
||||||
|
(String line) {
|
||||||
|
print('$line: ${line.length} bytes');
|
||||||
|
},
|
||||||
|
onDone: () {
|
||||||
|
print('File is now closed.');
|
||||||
|
},
|
||||||
|
onError: (e) {
|
||||||
|
print(e.toString());
|
||||||
|
},
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user