Reading a file as a String in Dart

It took me a minute to work out how to read a file in Dart (vm), so here it is for posterity:

void main() {
  File f = new File('my_filename.txt');
  FileInputStream fileStream = f.openInputStream();
  StringInputStream stringStream = new StringInputStream(fileStream);
  print(stringStream.read());
  fileStream.close();
}

Notes:
  • A StringInputStream reads strings from an InputStream
  • A FileInputStream is an implementor of InputStream that is opened from files
  • Remember to close the FileInputStream
Comments on Google+





Popular posts from this blog

PyGTK, Py2exe, and Inno setup for single-file Windows installers

ESP-IDF for Arduino Users, Tutorials Part 2: Delay

How to install IronPython2 with Mono on Ubuntu