dart - Reading text file using readAsString() and using the result -
the following example (1) reads file , prints contents without explicitly assigning file contents variable (ie. “.then(stdout.write)”). however, if want more print contents (2), need assign contents variable (i think).
is possible achieve (print contents , more), without assigning text of file variable?
in first example, implicit variable created? or, put way, example1 use less resources not creating explicit variable?
//example 1: import 'dart:io'; void main() { new file(new options().script) .readasstring(encoding: encoding.ascii) .then(stdout.write) .catcherror((oerror) => print(oerror)); print("reading file ...\n"); } //example 2: import 'dart:io'; void main() { new file(new options().script) .readasstring(encoding: encoding.ascii) .then((string stext) { stdout.write(stext+"\n\n"); print ('completed'); }) .catcherror((oerror) => print(oerror)); print("reading file ...\n"); }
in first example, this:
.then(stdout.write)
is equivalent this:
.then((string stext) { stdout.write(stext); })
technically there's 1 more function call, , have 1 more variable, should cost few bytes (i'm not sure on exact implementation). strings immutable; receiving reference string, not saving resources (other function call , few bytes of memory) using second version.
whatever want contents of string involve using resources, of course, shouldn't issue unless file huge.
Comments
Post a Comment