Write to a file, ends up with zero bytes

62 views
Skip to first unread message

Jon Kleiser

unread,
Apr 4, 2017, 2:58:44 PM4/4/17
to Dart Misc
Hi,

I want to write some text to a file, and I have tried both methods given at https://api.dartlang.org/stable/1.22.1/dart-io/File-class.html, both the one using "writeAsString" and the one using "sink.write", but both ways I end up with a completely empty file, zero bytes. Why is that happening?

/Jon

Istvan Soos

unread,
Apr 4, 2017, 3:06:30 PM4/4/17
to General Dart Discussion, jon.k...@usit.uio.no
Hi Jon,

It might be possible that you are starting an async IO operation and
your program exits before it gets done. The following code should
work, because it awaits for the writeAsString() to be completed:

import 'dart:io';

main() async {
await new File('output.txt').writeAsString('content');
}

This article has a few more examples about Dart's IO uses:
https://www.dartlang.org/articles/dart-vm/io
Hope it helps.

Istvan
> --
> For other discussions, see https://groups.google.com/a/dartlang.org/
>
> For HOWTO questions, visit http://stackoverflow.com/tags/dart
>
> To file a bug report or feature request, go to http://www.dartbug.com/new
> ---
> You received this message because you are subscribed to the Google Groups
> "Dart Misc" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to misc+uns...@dartlang.org.

Jon Kleiser

unread,
Apr 4, 2017, 4:09:10 PM4/4/17
to Dart Misc, jon.k...@usit.uio.no
Thanks, Istvan. Yes, putting "await" in front of "new File" looks like it's the way to go. I just have to figure out a nice way to do the rest of the printing, unless I shall give the total content as one big string to 'writeAsString'.

/Jon

Istvan Soos

unread,
Apr 4, 2017, 4:16:16 PM4/4/17
to General Dart Discussion, jon.k...@usit.uio.no
Keep in mind that the await is not "before" the "new File()", my code
was the short version of:

Future main() async {
File file = new File('output.txt');
Future f = file.writeAsString('content');
await f;

Filip Hracek

unread,
Apr 4, 2017, 4:58:38 PM4/4/17
to mi...@dartlang.org
If you want to write to the file part-by-part (instead of one huge String), you can use File.sink. Just make sure you close it afterwards.

Bob Nystrom

unread,
Apr 4, 2017, 6:24:47 PM4/4/17
to General Dart Discussion, jon.k...@usit.uio.no
On Tue, Apr 4, 2017 at 1:09 PM, Jon Kleiser <jon.k...@usit.uio.no> wrote:
Thanks, Istvan. Yes, putting "await" in front of "new File" looks like it's the way to go. I just have to figure out a nice way to do the rest of the printing, unless I shall give the total content as one big string to 'writeAsString'.

Note that File also supports synchronous versions of most of its methods. If you don't need the asynchrony, I would recommend using the sync ones. So, in this case, you can just use writeAsStringSync().

Cheers!

– bob

Jon Kleiser

unread,
Apr 5, 2017, 4:03:00 AM4/5/17
to Dart Misc, jon.k...@usit.uio.no
Thanks, all of you. I ended up with this:

    var randAccFile = new File(filename).openSync(mode: FileMode.WRITE_ONLY);
    randAccFile.writeStringSync("label\n");
    args.forEach((word) {
      randAccFile.writeStringSync("$word\n");
    });
    randAccFile.closeSync();

/Jon
Reply all
Reply to author
Forward
0 new messages