我想创建一个文件,比如foo/bar/baz/bleh.html,但foofoo/bar/等目录都不存在.

我如何创建我的文件递归创建所有的目录沿途?

推荐答案

简单的代码:

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');

Dart相关问答推荐

为什么我们在dart 里有工具?

触发从小部件到状态对象的函数

Dart中有没有类似python 字典(dictionary) struct ?

执行 `dart2js` 时会生成哪些文件?为什么?

flutter pub run build_runner 卡住了

如何为 spawnUri 动态构建 Dart 脚本?

我需要在使用之前将我的 Dart 包发布到 pub.dartlang.org 吗?

IOS中Webview Flutter字体太小

如何在Flatter中将图表线 colored颜色 更改为自定义 colored颜色 代码值

Flutter热重新加载和热重启不工作

多级异步代码中的 Dart 错误

Flutter列表视图底部溢出

如何在Dart中处理JSON

Flutter DataTable - 点击行

Flutter 构建失败. Android 依赖 'androidx.core:core' 的编译(1.0.0)和运行时(1.0.1)类路径有不同的版本

CoffeeScript 和 Dart 对 JavaScript 的改进之间的主要区别是什么?

Dart 的服务器端框架

找不到名为pubspec.yaml的文件

Dart中var和dynamic类型的区别?

如何在 Dart 中生成随机数?