33

I want to create a file, say foo/bar/baz/bleh.html, but none of the directories foo, foo/bar/, etc. exist.

How do I create my file recursively creating all of the directories along the way?

Seth Ladd
  • 112,095
  • 66
  • 196
  • 279
Juniper Belmont
  • 3,296
  • 2
  • 18
  • 28

3 Answers3

39

Alternatively:

new File('path/to/file').create(recursive: true);

Or:

new File('path/to/file').create(recursive: true)
.then((File file) {
  // Stuff to do after file has been created...
});

Recursive means that if the file or path doesn't exist, then it will be created. See: https://api.dartlang.org/apidocs/channels/stable/dartdoc-viewer/dart-io.File#id_create

EDIT: This way new Directory doesn't need to be called! You can also do this in a synchronous way if you so choose:

new File('path/to/file').createSync(recursive: true);
Will Squire
  • 6,127
  • 7
  • 45
  • 57
  • 1
    This is basically the same answer as the one from @JuniperBelmont with the distinction that `create` is used instead of `createSync`. Using the async API isn't always more efficient as a recent discussion showed especially for action that don't involve time consuming operations (full discussion: https://groups.google.com/a/dartlang.org/forum/#!topic/misc/uWy-rO5sz_k) – Günter Zöchbauer Oct 11 '14 at 14:17
  • 2
    The difference I was trying to get at here is you don't need to call new Directory to create the non-existent directories. Simply calling the create method on File with the recursive argument of true does it for you. Anyway, I thought it looked a bit cleaner – Will Squire Oct 11 '14 at 14:45
13

Simple code:

import 'dart:io';

void createFileRecursively(String filename) {
  // Create a new directory, recursively creating non-existent directories.
  new Directory.fromPath(new Path(filename).directoryPath)
      .createSync(recursive: true);
  new File(filename).createSync();
}

createFileRecursively('foo/bar/baz/bleh.html');
Shailen Tuli
  • 13,815
  • 5
  • 40
  • 51
Juniper Belmont
  • 3,296
  • 2
  • 18
  • 28
  • 1
    I'd be curious how you specify the file permissions on those directories. It doesn't appear they necessarily inherit from the parent. – sager89 Oct 19 '16 at 19:39
  • As of may 2019, it's still not possible to set file permissions with Dart. See: https://github.com/dart-lang/sdk/issues/15078 – CedX May 09 '19 at 20:45
9

Here's how to create, read, write, and delete files in Dart:

Creating files:

import 'dart:io';

main() {
 new File('path/to/sample.txt').create(recursive: true);
}

Reading files:

import 'dart:io';

Future main() async {
 var myFile = File('path/to/sample.txt');
 var contents;
 contents = await myFile.readAsString();
 print(contents);
}

Writing to files:

import 'dart:io';

Future main() async {
 var myFile = File('path/to/sample.txt');
 var sink = myFile.openWrite(); // for appending at the end of file, pass parameter (mode: FileMode.append) to openWrite()
 sink.write('hello file!');
 await sink.flush();
 await sink.close();
}

Deleting files:

import 'dart:io';

main() {
 new File('path/to/sample.txt').delete(recursive: true);
}

Note: All of the above code works properly as of Dart 2.7