-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
need higher level read/write apis in dart:io for scripting #1579
Comments
Also RandomAccessFile has writeStringSync but not readStringSync |
I agree, we need to provide shortcuts for reading an entire file as a string. Thanks for filing the bug report. cc @sgjesse. |
Removed Area-Library label. |
Added Started label. |
Landed File.{readAsBytes, readAsText, readAsLines}. Example usage: String text = new File('myfile.txt').readAsTextSync('UTF-8'); var f = new File('myfile.txt'); /** /** /** /** /** /** Added Fixed label. |
This comment was originally written by ama...@gmail.com Why readAsBytes, readAsText and readAsLuines does not return promises? |
None of the APIs in dart:io do. You can easily build that on top (this code is not tested): class FutureFile { Future<List<int>> readAsBytes() { |
This comment was originally written by amatia...@gmail.com I supposed "higher level" mean I haven't to do things like that. I don't like it because when I hear "higher level" I thought I could read a file with a line: new File(path).readAsBytes().then((data) => ... ); With that implementation I need at least three lines. var f = new File(path); |
This comment was originally written by amatias...@gmail.com That means I can't do this inside a arrow-function, compare div.on.click.add(() => div.on.click.add(() { |
Why doesn't File use Future? |
To keep the dart:io interfaces consistent. We are using an evented model because we need repeatedly firing data events for the streaming APIs. In order to have a consistent API we are using events and 'handlers' for everything. This is a fairly low-level API on which we can built other abstractions later. |
This comment was originally written by amatiasq...@gmail.com I asked for a high-level library to not be forced to build abstractions everywhere I use it. If I have to build a abstraction for this I dont need this abstraction from low-level. Why the high-level API has to be consistent with low-level? If we ask for a high-level it's because low-level API isn't high-level enought. |
This comment was originally written by @seaneagan @ager see my comment in issue #1786 for why this is an undesirable form of consistency. If anything, the consistency should be between dart:io and the APIs provided for this exact purpose in dart:core (Future and possibly Event (see issue #1873)). |
Removed Area-IO label. |
RandomAccessFile is a nice low-level building block, but it'd be really great to have some convenience methods.
For example .NET has APIs like[1]:
class File {
static FileStream Open(String path, [FileMode mode]);
static String ReadAllText(String path, [Encoding encoding]);
static String[] ReadAllLines(String path, [Encoding encoding]);
static byte[] ReadAllBytes(String path);
// ... and various equivalents for writing, and lots more ...
}
Usage is very simple:
String theData = File.ReadAllText('path/to/data.txt');
Python[2], Ruby[3] also have very simple ways to do IO:
# Python
f = open('path/to/data.txt', 'r')
lines = f.readlines()
f.close()
# Ruby
IO.foreach('path/to/data.txt') { |line| puts line }
[1] http://msdn.microsoft.com/en-us/library/3z2ck8eh.aspx
[2] http://docs.python.org/tutorial/inputoutput.html
[3] http://ruby-doc.org/core-1.9.3/IO.html#method-c-foreach
The text was updated successfully, but these errors were encountered: