Dart: path库

path库pub地址

安装:

dependencies:
  path:

使用:

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

main(List<String> args) async {
  print(Directory.current.path); // D:\ajanuw\dart-test 

  print(path.joinAll([Directory.current.path, 'bin', 'main.dart'])); // 拼接一个路径:D:\ajanuw\dart-test\bin\main.dart
  String __filename = Platform.script.path.replaceFirst('/', ''); // 脚本路径:D:/ajanuw/dart-test/bin/main.dart
  String __dirname = path.dirname(__filename); // 脚本目录:D:/ajanuw/dart-test/bin
  print(__filename);
  print(__dirname);
  String a = path.joinAll([__dirname, '..', 'test', 'dart_test_test.dart']); // 拼接路径:D:/ajanuw/dart-test/bin\..\test\dart_test_test.dart
  print(a);
  print(await File(a).exists()); // 文件是否存在
}

执行:

D:\ajanuw\dart-test>dart ./bin/main.dart
D:\ajanuw\dart-test
D:\ajanuw\dart-test\bin\main.dart
D:/ajanuw/dart-test/bin/main.dart
D:/ajanuw/dart-test/bin
D:/ajanuw/dart-test/bin\..\test\dart_test_test.dart
true

D:/ajanuw/dart-test/bin\..\test\dart_test_test.dart 虽然这个路径的分隔符乱七八糟的但还是能够找到呢!

规范化[path]

  String p = 'D:/ajanuw/dart-test/bin\\..\\test\\dart_test_test.dart';
  print(p);
  print(path.normalize(p)); // 尽可能删除冗余路径分隔符
  print(path.canonicalize(p)); // 规范化

basename

path.basename('path/to/'); // -> 'to'
path.basename('path/to/a.txt') // -> a.txt
path.basenameWithoutExtension('path/to/a.txt') // -> a,获取最后一个分隔符之后的[path]部分,没有任何后缀

extension

获取[path]的文件扩展名:[basename]的最后一部分, 包括.本身

path.extension('path/to/a.txt') // -> .txt

rootPrefix

返回[path]的根,如果它是绝对的, 如果它是相对的返回空字符串

///     // Unix
///     p.rootPrefix('path/to/foo'); // -> ''
///     p.rootPrefix('/path/to/foo'); // -> '/'
///
///     // Windows
///     p.rootPrefix(r'path\to\foo'); // -> ''
///     p.rootPrefix(r'C:\path\to\foo'); // -> r'C:\'
///
///     // URL
///     p.rootPrefix('path/to/foo'); // -> ''
///     p.rootPrefix('http://dartlang.org/path/to/foo');
///       // -> 'http://dartlang.org'

split

使用当前平台的[separator]将[path]拆分为其组件

///     p.split('path/to/foo'); // -> ['path', 'to', 'foo']
///
///   在分割之前,路径将不被标准化
///
///     p.split('path/../foo'); // -> ['path', '..', 'foo']
///
///   如果[path]是绝对的,则根目录将是该目录中的第一个元素
///   array. Example:
///
///     // Unix
///     p.split('/path/to/foo'); // -> ['/', 'path', 'to', 'foo']
///
///     // Windows
///     p.split(r'C:\path\to\foo'); // -> [r'C:\', 'path', 'to', 'foo']
///
///     // Browser
///     p.split('http://dartlang.org/path/to/foo');
///       // -> ['http://dartlang.org', 'path', 'to', 'foo']

relative

尝试将[path]转换为当前的等效相对路径

///     // Given current directory is /root/path:
///     p.relative('/root/path/a/b.dart'); // -> 'a/b.dart'
///     p.relative('/root/other.dart'); // -> '../other.dart'
///
/// If the [from] argument is passed, [path] is made relative to that instead.
///
///     p.relative('/root/path/a/b.dart', from: '/root/path'); // -> 'a/b.dart'
///     p.relative('/root/other.dart', from: '/root/path');
///       // -> '../other.dart'
///
/// If [path] and/or [from] are relative paths, they are assumed to be relative
/// to the current directory.
///
/// Since there is no relative path from one drive letter to another on Windows,
/// or from one hostname to another for URLs, this will return an absolute path
/// in those cases.
///
///     // Windows
///     p.relative(r'D:\other', from: r'C:\home'); // -> 'D:\other'
///
///     // URL
///     p.relative('http://dartlang.org', from: 'http://pub.dartlang.org');
///       // -> 'http://dartlang.org'


print(path.relative(Platform.script.path.replaceFirst('/', '')));
ajanuw@ajanuw /d/ajanuw/dart-test
λ dart bin/main.dart
bin\main.dart

isWithin(String parent, String child)

如果[child]是“parent”下面的路径,则返回“true”,否则返回“false”

p.isWithin('/root/path', '/root/path/a'); // -> true
p.isWithin('/root/path', '/root/other'); // -> false
p.isWithin('/root/path', '/root/path') // -> false

equals(String path1, String path2)

如果[path1]指向与[path2]相同的位置,则返回“true”,否则false

path.equals('a/b', 'a/b')  -> true 

withoutExtension

从[path]的最后一部分删除尾随扩展名

p.withoutExtension('path/to/foo.dart'); // -> 'path/to/foo'

setExtension

返回[path],尾随扩展名设置为[extension]

p.setExtension('path/to/foo.dart', '.js') // -> 'path/to/foo.js'
p.setExtension('path/to/foo.dart.js', '.map') // -> 'path/to/foo.dart.map'
p.setExtension('path/to/foo', '.js') // -> 'path/to/foo.js'

fromUri

返回[uri]表示的路径,可以是[String]或[Uri]

// POSIX
p.fromUri('file:///path/to/foo') // -> '/path/to/foo'

// Windows
p.fromUri('file:///C:/path/to/foo') // -> r'C:\path\to\foo'

 // URL
p.fromUri('http://dartlang.org/path/to/foo') // -> 'http://dartlang.org/path/to/foo'

// 相对路径返回相对路径
p.fromUri('path/to/foo'); // -> 'path/to/foo'

prettyUri

返回[uri]的简洁,人类可读的表示

  print(path.relative('d:/ajanuw/dart-test/a/b.dart'));
  print(path.prettyUri('file:///d:/ajanuw/dart-test/a/b.dart'));

ajanuw@ajanuw /d/ajanuw/dart-test
λ dart bin/main.dart
a\b.dart
a\b.dart
bool isAbsolute(String path)  如果[path]是相对路径则返回“true”,如果是绝对路径则返回“false”
bool isRelative(String path) 如果[path]是根相对路径则返回“true”,如果不是,则返回“false”
bool isRootRelative(String path)  没看懂干啥的
Uri toUri(String path)   Uri.parse
p.separator  获取当前平台的路径分隔符。 `\` 这是Windows上的
p.current  返回工作路径
p.absolute('a', 'b')  p.current+a/b
posted @ 2019-05-24 11:06  Ajanuw  阅读(1468)  评论(0编辑  收藏  举报