How to read a file line by line in Dart

前端 未结 4 821
深忆病人
深忆病人 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<List<String>> stream = 
          new Stream.fromFuture(new File('Data.txt').readAsLines());
    

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

      List<String> lines = new File('Data.txt').readAsLinesSync();
      for (var line in lines) {
        stdout.writeln(line); 
      } 
    
    0 讨论(0)
  • 2021-01-19 17:32

    The converter's startChunkedConversion is only called once, when the transformation is started. However, the returned sink's add method is invoked multiple times with parts of the file.

    It's up to the source to decide how big the chunks are, but a 37MB file (as mentioned in your previous question) will definitely be sent in smaller chunks.

    If you want to see the chunks you can either intercept startChunkedConversion and return a wrapped sink, or you can put yourself between the openRead and the transformer.

    Intercept:

    class InterceptSink {
      static int lineSplitCount = 0;
    
      final _sink;
      InterceptSink(this._sink);
      add(x) {
        print("InterceptSink.add Count: $lineSplitCount");
        lineSplitCount++;
        _sink.add(x);
      }
      close() { _sink.close(); }
    }
    
    class LineSplitterIntercept extends Converter {
      convert(x) { throw "unimplemented"; }
      startChunkedConversion(outSink) {
        var lineSink = new LineSplitter().startChunkedConversion(outSink);
        return new InterceptSink(lineSink);
      }
    }
    

    After openRead:

    file.openRead()
      .transform(UTF8.decoder)
      .map(x) {
        print("chunk size: ${x.length)");
        return x;
      }
      .transform(new LineSplitter())
      ...
    
    0 讨论(0)
  • 2021-01-19 17:45

    I think this code is useful:

    import 'dart:io';
    import 'dart:convert';
    import 'dart:async';
    
    main() {
      final file = new File('file.txt');
      Stream<List<int>> inputStream = file.openRead();
    
      inputStream
        .transform(utf8.decoder)       // Decode bytes to UTF-8.
        .transform(new LineSplitter()) // Convert stream to individual lines.
        .listen((String line) {        // Process results.
            print('$line: ${line.length} bytes');
          },
          onDone: () { print('File is now closed.'); },
          onError: (e) { print(e.toString()); });
    }
    
    0 讨论(0)
  • 2021-01-19 17:49

    Because none of the other answers suited my situation, here is another technique:

    import 'dart:io';
    import 'dart:convert';
    
    
    void main()
    {
        var file = File('/path/to/some/file.txt);
        var raf = file.openSync(mode: fileMode.read);
    
        String line;
        while ((line = readLine(raf)) != null)
        {
            print(line);
        }
      }
    
      String readLine(RandomAccessFile raf, {String lineDelimiter = '\n'}) {
    
        var line = '';
        int byte;
        var priorChar = '';
    
        var foundDelimiter = false;
    
        while ((byte = raf.readByteSync()) != -1) {
          var char = utf8.decode([byte]);
    
          if (isLineDelimiter(priorChar, char, lineDelimiter)) {
            foundDelimiter = true;
            break;
          }
    
          line += char;
          priorChar = char;
        }
        if (line.isEmpty && foundDelimiter == false) {
          line = null;
        }
        return line;
      }
    
    
      bool isLineDelimiter(String priorChar, String char, String lineDelimiter) {
        if (lineDelimiter.length == 1) {
          return char == lineDelimiter;
        } else {
          return priorChar + char == lineDelimiter;
        }
      }
    
    0 讨论(0)
提交回复
热议问题