2
votes

I'm writing a dart based application. I want to read from and write to a server side file. Every read operation should read the last line of the file and the write operations should write to the end of the file. How would I do this?

To give a better idea, in Python I'd do it as:

with open('foo.txt', 'w+') as f:
    f.readline()
    f.write('baz')

UPDATE: This is a browser based Dart app. Apparently dart:io does not work with browser-based apps.

1

1 Answers

1
votes
import 'dart:io';

readLastLine(File f) => f.readAsLinesSync().last;
append(File f, String contents) =>
    f.writeAsStringSync(contents, mode: FileMode.APPEND);

NB: UTF8 is used as default encoding. You can change it with the optional named parameter encoding on readAsLinesSync and writeAsStringSync.