How to read a file line by line in Dart

前端 未结 4 823
深忆病人
深忆病人 2021-01-19 16:50

This question is a continuation of a previous question. I wrote the following piece of code to determine if File.openRead() created a Stream that could be stre

4条回答
  •  执念已碎
    2021-01-19 17:24

    If a stream is necessary, you can create it from the future that readAsLines() returns:

       Stream> stream = 
          new Stream.fromFuture(new File('Data.txt').readAsLines());
    

    However it looks simpler to me to plainly process the lines one by one,

      List lines = new File('Data.txt').readAsLinesSync();
      for (var line in lines) {
        stdout.writeln(line); 
      } 
    

提交回复
热议问题