I have a relatively big .csv
file and already 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.
Asked
Active
Viewed 886 times
2

doneforaiur
- 1,308
- 7
- 14
- 21
-
Does this answer your question? [Reading file line by line in dart](https://stackoverflow.com/questions/21813401/reading-file-line-by-line-in-dart) – Kevin Mar 30 '22 at 12:11
-
Hey @Kevin unfortunately no. https://api.dart.dev/stable/2.16.2/dart-io/File/openRead.html It loads the entire document if start and end is not specified. The memory usage increased as much as the file, which is undesirable but if there's no other way I'll stick to this solution. – doneforaiur Mar 30 '22 at 12:20
1 Answers
3
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'));
}

user3185563
- 1,314
- 2
- 15
- 22