This Blog continues on http://aliafshar.github.io/blog

Thursday, December 08, 2011

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+