Discussion:
[dart-misc] Write to a file, ends up with zero bytes
Jon Kleiser
2017-04-04 18:58:44 UTC
Permalink
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
--
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+***@dartlang.org.
Istvan Soos
2017-04-04 19:06:07 UTC
Permalink
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
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
--
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
--
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+***@dartlang.org.
Jon Kleiser
2017-04-04 20:09:10 UTC
Permalink
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
Post by Istvan Soos
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
import 'dart:io';
main() async {
await new File('output.txt').writeAsString('content');
}
https://www.dartlang.org/articles/dart-vm/io
Hope it helps.
Istvan
Post by Jon Kleiser
Hi,
I want to write some text to a file, and I have tried both methods given
at
Post by Jon Kleiser
https://api.dartlang.org/stable/1.22.1/dart-io/File-class.html, both
the one
Post by Jon Kleiser
using "writeAsString" and the one using "sink.write", but both ways I
end up
Post by Jon Kleiser
with a completely empty file, zero bytes. Why is that happening?
/Jon
--
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
Post by Jon Kleiser
---
You received this message because you are subscribed to the Google
Groups
Post by Jon Kleiser
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send
an
--
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+***@dartlang.org.
Istvan Soos
2017-04-04 20:15:51 UTC
Permalink
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;
}
Post by Jon Kleiser
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
Post by Istvan Soos
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
import 'dart:io';
main() async {
await new File('output.txt').writeAsString('content');
}
https://www.dartlang.org/articles/dart-vm/io
Hope it helps.
Istvan
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
--
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
--
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
--
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+***@dartlang.org.
'Filip Hracek' via Dart Misc
2017-04-04 20:57:54 UTC
Permalink
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.

Filip Hráček
https://google.com/+filiphracek
Post by Jon Kleiser
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
Post by Istvan Soos
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
import 'dart:io';
main() async {
await new File('output.txt').writeAsString('content');
}
https://www.dartlang.org/articles/dart-vm/io
Hope it helps.
Istvan
Post by Jon Kleiser
Hi,
I want to write some text to a file, and I have tried both methods
given at
Post by Jon Kleiser
https://api.dartlang.org/stable/1.22.1/dart-io/File-class.html, both
the one
Post by Jon Kleiser
using "writeAsString" and the one using "sink.write", but both ways I
end up
Post by Jon Kleiser
with a completely empty file, zero bytes. Why is that happening?
/Jon
--
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
Post by Jon Kleiser
---
You received this message because you are subscribed to the Google
Groups
Post by Jon Kleiser
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send
an
--
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
--
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+***@dartlang.org.
'Bob Nystrom' via Dart Misc
2017-04-04 22:24:12 UTC
Permalink
Post by Jon Kleiser
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()
<https://api.dartlang.org/stable/1.22.1/dart-io/File/writeAsStringSync.html>
.

Cheers!

– bob
Post by Jon Kleiser
/Jon
Post by Istvan Soos
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
import 'dart:io';
main() async {
await new File('output.txt').writeAsString('content');
}
https://www.dartlang.org/articles/dart-vm/io
Hope it helps.
Istvan
Post by Jon Kleiser
Hi,
I want to write some text to a file, and I have tried both methods
given at
Post by Jon Kleiser
https://api.dartlang.org/stable/1.22.1/dart-io/File-class.html, both
the one
Post by Jon Kleiser
using "writeAsString" and the one using "sink.write", but both ways I
end up
Post by Jon Kleiser
with a completely empty file, zero bytes. Why is that happening?
/Jon
--
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
Post by Jon Kleiser
---
You received this message because you are subscribed to the Google
Groups
Post by Jon Kleiser
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send
an
--
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
--
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+***@dartlang.org.
Jon Kleiser
2017-04-05 08:02:59 UTC
Permalink
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
Post by 'Bob Nystrom' via Dart Misc
Post by Jon Kleiser
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()
<https://api.dartlang.org/stable/1.22.1/dart-io/File/writeAsStringSync.html>
.
Cheers!
– bob
Post by Jon Kleiser
/Jon
Post by Istvan Soos
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
import 'dart:io';
main() async {
await new File('output.txt').writeAsString('content');
}
https://www.dartlang.org/articles/dart-vm/io
Hope it helps.
Istvan
Post by Jon Kleiser
Hi,
I want to write some text to a file, and I have tried both methods
given at
Post by Jon Kleiser
https://api.dartlang.org/stable/1.22.1/dart-io/File-class.html, both
the one
Post by Jon Kleiser
using "writeAsString" and the one using "sink.write", but both ways I
end up
Post by Jon Kleiser
with a completely empty file, zero bytes. Why is that happening?
/Jon
--
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
Post by Jon Kleiser
---
You received this message because you are subscribed to the Google
Groups
Post by Jon Kleiser
"Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send
an
--
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
--
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+***@dartlang.org.
Continue reading on narkive:
Loading...