Reading CSV file line by line in Dart without loading the entire file

Viewed 286

I have a relatively big .csv file and tried readAsLines and readAsLinesSync methods. But these methods reads the entire file. What I really need is a method which skips N lines and returns specified number of lines without wasting too much system resources.

The main reason for this request is that a) the file might be updated after the initial read and b) save some system resources.

1 Answers

As described here, you can read large files asynchronously, using a stream, like this:

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'));
}
Related