0

I have a large .csv file where I need to edit a single value. Is there a way to do that without writing the entire file again?

I know that I can read a file line-by-line like this:

await for (var event in file
      .openRead()
      .transform(utf8.decoder)
      .transform(LineSplitter())) {
    if (isLineToEdit) {
      //edit the line
      break;
    }
  }

However I don't know a way to edit a single line and write that to the file without having to write the entire file again. As far as I know, the only way to write to a file is by either overwriting it (FileMode.write) or by appending at the end (FileMode.append). Am I missing something? Is there some way to do that in dart?

Smofe
  • 71
  • 2
  • 4
    It is technically possible to modify a file in-place, using system calls (I have no idea if this is possible from Dart). But 1) you need to know where to modify, 2) you can't make the replacement longer than the original as this would require to shift the tail, 3) you need to keep the content valid csv data. –  Dec 28 '22 at 10:03
  • 3
    https://api.flutter.dev/flutter/dart-io/RandomAccessFile-class.html but as Yves already said you cannot neither shrink nor enlarge your file during edit – pskink Dec 28 '22 at 10:14
  • How big is your file? – mezoni Aug 10 '23 at 21:29

0 Answers0