Reading a file line by line in a dart

I am trying to process large text files in the Dart language. Files are larger than 100 MB.

I tried methods readAsLinesand readAsLinesSynclibraries dart:io. Every time I'm running memory Exhausted heap space.

Is there a way to read a file line by line or byte by bytes, as in other languages?

+5
source share
2 answers

This should read the file in chunks:

import 'dart:async';
import 'dart:io';
import 'dart:convert';

main() {
  var path = ...;
  new File(path)
    .openRead()
    .transform(UTF8.decoder)
    .transform(new LineSplitter())
    .forEach((l) => print('line: $l'));
}

There is little documentation about this yet. Perhaps register a bug requesting more documents.

+8
source

In the latest version of dart, the encoder UTF8.decoder is now called utf8.decoder:

import 'dart:async';
import 'dart:io';
import 'dart:convert';

main() {
  var path = ...;
  new File(path)
    .openRead()
    .transform(utf8.decoder)
    .transform(new LineSplitter())
    .forEach((l) => print('line: $l'));
}
0
source

Source: https://habr.com/ru/post/1527200/


All Articles