【问题标题】:How to get the file if I know the root directory and relative path?如果我知道根目录和相对路径,如何获取文件?
【发布时间】:2014-03-03 14:19:08
【问题描述】:

在Dart中,如果我知道一个文件的根目录和相对路径,如何为它创建一个文件实例?

Directory root = new Directory("/root");
String relativePath = "logs/users.log";

如何为users.log创建文件实例?

在java中,很简单:

new File(root, relativePath);

但在 Dart 中,我找不到这么简单的解决方案。

【问题讨论】:

  • 这在 Dart SDK 中是不可能的。您必须使用第三方软件。甚至 Dart SDK 也为此使用了外部包。这个事实看起来很奇怪,因为 Dart SDK 甚至连最简单的原生支持都没有。在这种情况下,这意味着您不能用 Dart 语言编写(单文件命令行)脚本,因为这需要您使用其他包。当然,您可以将您的应用程序(根脚本及其依赖项)编译为快照,但这与您将其编写为具有可读源代码的(单个文件)命令行脚本不同。

标签: file dart dart-io


【解决方案1】:

这是我找到的最简单的解决方案

import 'package:path/path.dart' as path;

...

String filePath = path.join(root.path, relativePath);
filePath = path.normalize(filePath);
File f = new File(filePath);

【讨论】:

    【解决方案2】:

    加入 /home/name/../name2 以产生 /home/name2

    编辑:

    感谢 Günter Zöchbauer 的提示。
    看来linux盒子可以处理/home/name/../name2之类的路径。

    在 Windows 机器上,需要使用 Path.normalize,并且必须删除头部额外的 / Path.normalize 前缀。

    或者使用 new Path.Context():

    import 'package:path/path.dart' as Path;
    import 'dart:io' show Platform,Directory;
    
    to_abs_path(path,[base_dir = null]){
      Path.Context context;
      if(Platform.isWindows){
        context = new Path.Context(style:Path.Style.windows);
      }else{
        context = new Path.Context(style:Path.Style.posix);
      }
      base_dir ??= Path.dirname(Platform.script.toFilePath());
      path = context.join( base_dir,path);
      return context.normalize(path);
    }
    

    【讨论】:

    • path.join() 对我来说适用于相对路径。如果您想摆脱../,只需致电relativePath = path.normalize(relativePath);。另请参阅我的更新答案。
    • 嗨,君特·佐赫鲍尔。感谢您提供有用的提示。这是一个平台问题。 new Path.Context 似乎是最干净的解决方案。
    • 不知道Context。感谢分享:)
    【解决方案3】:

    我发现了在我的测试脚本文件中查找文件的相对路径的问题,因此我改进了@TastyCatFood 的答案,使其也可以在该上下文中工作。以下脚本可以在每个位置找到文件的相对文件:

    import 'dart:io';
    import 'package:path/path.dart' as path;
    
    ///  Find the path to the file given a name
    ///  [fileName] : file name
    ///  [baseDir] : optional, base directory to the file, if not informed, get current script path.
    String retrieveFilePath(String fileName, [String baseDir]){
      var context;
      // get platform context
      if(Platform.isWindows) {
        context = path.Context(style:path.Style.windows);
      } else {
        context = path.Context(style:path.Style.posix);
      }
    
      // case baseDir not informed, get current script dir
      baseDir ??= path.dirname(Platform.script.path);
      // join dirPath with fileName
      var filePath = context.join(baseDir, fileName);
      // convert Uri to String to make the string treatment more easy
      filePath = context.fromUri(context.normalize(filePath));
      // remove possibles extra paths generated by testing routines
      filePath = path.fromUri(filePath).split('file:').last;
    
      return filePath;
    }
    

    以下示例读取main.dart文件同一文件夹下的data.txt文件:

    import 'package:scidart/io/io.dart';
    
    main(List<String> arguments) async {
       File f = new File('data.txt');
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-30
      • 2013-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多