Dart的文件、目录和链接(3)

简介: <div class="markdown_views"><h2 id="创建目录">创建目录</h2><pre class="prettyprint"><code class=" hljs coffeescript"><span class="hljs-reserved">import</span> <span class="hljs-string">'dart:io'<

创建目录

import 'dart:io';

main(List<String> arguments) {
  new Directory('dir/subdir').create(recursive: true)
    .then((Directory directory) {
      print(directory.path);
    });
}

执行后会在项目中新建目录

这里写图片描述

创建临时目录

import 'dart:io';

main(List<String> arguments) {
  Directory.systemTemp.createTemp('my_temp_dir')
    .then((directory) {
      print(directory.path);
    });
}

执行后会在系统临时目录下创建一个临时目录

这里写图片描述

列出目录的清单

import 'dart:io';

main(List<String> arguments) {
  var systemTempDir = Directory.systemTemp;
  //列出目录内容(递归到子目录),不包含链接
  systemTempDir.list(recursive: true, followLinks: false)
    .listen((FileSystemEntity entity) {
      print(entity.path);
    });
}

在控制台列出目录清单

这里写图片描述

创建链接

import 'dart:io';

main(List<String> arguments) {
  var systemTempDir = Directory.systemTemp;
  //在系统临时目录里创建一个临时目录
  systemTempDir.createTemp('my_temp_dir').then((temp) {
    //Platform.pathSeparator获取系统的路径分隔符
    var first = '${temp.path}${Platform.pathSeparator}first';
    var second = '${temp.path}${Platform.pathSeparator}second';
    //创建一个目录
    new Directory(first).create(recursive:true);
    //创建一个链接
    new Link(second).create(first);
  });
}

先看临时目录里的文件

这里写图片描述

现在点击second打开的却是firse,说明second是链接

检查路径是否链接

import 'dart:io';

main(List<String> arguments) {
  var systemTempDir = Directory.systemTemp;
  systemTempDir.list(recursive: true, followLinks: false)
    .listen((FileSystemEntity entity) {
      //检查路径是否为链接
      FileSystemEntity.isLink(entity.path).then((isLink) {
        //如果是链接就输出
        if (isLink) print(entity.path);
      });
    });
}

因为此时系统临时目录中只有刚刚创建的链接,所以只输出一条路径

这里写图片描述

查看链接的目标

import 'dart:async';
import 'dart:io';

Future<Link> createSymlink() {
  return Directory.systemTemp.createTemp('my_temp_dir').then((temp) {
    var first = '${temp.path}${Platform.pathSeparator}first';
    var second = '${temp.path}${Platform.pathSeparator}second';
    new Directory(first).create(recursive:true);
    return new Link(second).create(first);
  });
}

main(List<String> arguments) {
  createSymlink()
    .then((Link link) {
      print('链接的路径:${link.path}');
      link.target().then((String targetPath) {
        print('链接的目标路径$targetPath');
      })
      .catchError((e) {
        print(e.message);
      });
    });
}

输出链接的路径以及其目标路径

这里写图片描述

目录
相关文章
|
5月前
|
JavaScript Android开发
AutoJs4.1.0实战教程---js文件打包发布成APK文件
AutoJs4.1.0实战教程---js文件打包发布成APK文件
890 0
AutoJs4.1.0实战教程---js文件打包发布成APK文件
「译文」如何在 Ansible 中复制多个文件和目录
「译文」如何在 Ansible 中复制多个文件和目录
|
5月前
|
JavaScript 前端开发 容器
如何使用Contentlayer和Tocbot创建博客网站目录?
如何使用Contentlayer和Tocbot创建博客网站目录?
72 0
|
C#
如何在 C# 项目中链接一个文件夹下的所有文件
在 C# 项目中通过链接方式引入文件可以让我们在项目中使用这些文件中的代码。常见的比如链接 AssemblyInfo.cs 文件,这样我们就可以在项目中使用这个文件中的版本号等信息。但是如果我们想要链接一个文件夹下的所有文件,该怎么做呢?今天我们就来看看如何在 C# 项目中链接一个文件夹下的所有文件。
123 0
如何在 C# 项目中链接一个文件夹下的所有文件
dirent--文件以及文件夹相关操作(跨平台)
dirent--文件以及文件夹相关操作(跨平台)
|
Java
使用代码把一个目录打包成jar
使用代码把一个目录打包成jar
139 0
下载xerces.jar链接
下载xerces.jar链接
81 0
编译编译时,用不到的库,一定不要链接
编译编译时,用不到的库,一定不要链接
103 0