如何从路径字符串中判断它是文件还是目录

时间:2014-08-20 10:41:51

标签: dart dart-io

当我有一个字符串路径时,无论路径指向文件还是目录,获取信息的最简单方法是什么。

1 个答案:

答案 0 :(得分:2)

import 'dart:io' as io;

void main() {
  print(io.Directory.current);
  String path = '../console/readline';

  var f = new io.File(path);
  print('exists: ${f.existsSync()}');
  print('file: ${f.statSync().type == io.FileSystemEntityType.FILE}');
  print('directory: ${f.statSync().type == io.FileSystemEntityType.DIRECTORY}');
  print('link: ${f.statSync().type == io.FileSystemEntityType.LINK}');
  print('not found: ${f.statSync().type == io.FileSystemEntityType.NOT_FOUND}');
}

打印现有目录

Directory: '/home/user/dart/playground/bin/io/get_entry_type_from_path'
exists: false
file: false
directory: true
link: false
not found: false