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

Dart: path库的更多相关文章

  1. Dart http库

    推荐下我写的一个http库ajanuw_http 最基本的获取数据 import 'package:http/http.dart' as http; main(List<String> a ...

  2. Dart自定义库、系统库和第三方库

    /* 前面介绍Dart基础知识的时候基本上都是在一个文件里面编写Dart代码的,但实际开发中不可能这么写,模块化很重要,所以这就需要使用到库的概念. 在Dart中,库的使用时通过import关键字引入 ...

  3. flutter-web利用dart js 库发起http request

    初学flutter,初学前端,尝试在dart中直接使用HttpClient时,直接报出Platform not supported,查资料发现他还不支持浏览器. 通过查阅资料发现可以借助axios 与 ...

  4. Dart: puppeteer库

    和node的差不多,只有写API不一样 puppeteer 地址 安装依赖 dependencies: puppeteer: ^1.7.1 下载 chrome-win 到 <project_ro ...

  5. Atitit.dart语言的特性  编译时js语言大总结

    Atitit.dart语言的特性  编译时js语言大总结 1. 原型环境1 1.1. Dart可以编译js3 2. 第二个期待的理由是Dart的语言特性,没有什么特别特性好像,类似java c#一小时 ...

  6. Dart学习笔记

    一.数据类型 1. 字符串 和 数字 互转 // String 转为 int '); assert(one == ); // String 转为 double var onePointOne = do ...

  7. 【dart学习】-- Dart之异步编程

    一,概述 编程中的代码执行,通常分为同步与异步两种. 同步:简单说,同步就是按照代码的编写顺序,从上到下依次执行,这也是最简单的我们最常接触的一种形式.但是同步代码的缺点也显而易见,如果其中某一行或几 ...

  8. Angular JS | Closure | Google Web Toolkit | Dart | Polymer 概要汇集

    AngularJS | Closure | Google Web Toolkit | Dart | Polymer GWT https://code.google.com/p/google-web-t ...

  9. [dart学习]第三篇:dart变量介绍 (二)

    本篇继续介绍dart变量类型,可参考前文:第二篇:dart变量介绍 (一) (一)final和const类型 如果你不打算修改一个变量的值,那么就把它定义为final或const类型.其中:final ...

随机推荐

  1. tarjan 复习笔记 割点与桥

    定义分析 给定一个无向连通图\(G=(V,E)\) 对于\(x\in Y\),如果删去\(x\)及与\(x\)相连的边后,\(G\)分裂为两个或者两个以上的不连通子图,那么称\(x\)为\(G\)的割 ...

  2. Spring Cloud系列之Commons - 1. 背景与基础知识准备

    本文基于 Spring Cloud 2020.0 发布版的依赖 本系列会深入分析 Spring Cloud 的每一个组件,从Spring Cloud Commons这个 Spring Cloud 所有 ...

  3. eclipse项目放到github

    一,下载git ,配置用户名和邮箱: git config --global user.name "name"       git config --global user.ema ...

  4. K8s 二、(1、kubeadm部署Kubernetes集群)

    准备工作 满足安装 Docker 项目所需的要求,比如 64 位的 Linux 操作系统.3.10 及以上的内核版本: x86 或者 ARM 架构均可: 机器之间网络互通,这是将来容器之间网络互通的前 ...

  5. python--基础2 (数据类型及应用)

    资源池 链接:https://pan.baidu.com/s/1OGq0GaVcAuYEk4F71v0RWw 提取码:h2sd python数据类型 字符串 列表 字典 数字(整数) 数字(浮点数) ...

  6. DoTween动画插件学习

    一.简单的变量插值运算 using System.Collections; using System.Collections.Generic; using UnityEngine; using DG. ...

  7. Codeforces Global Round 9 E. Inversion SwapSort

    题目链接:https://codeforces.com/contest/1375/problem/E 题意 给出一个大小为 $n$ 的数组 $a$,对数组中的所有逆序对进行排序,要求按照排序后的顺序交 ...

  8. 2019ICPC南昌邀请赛 Sequence

    题意:给出n个点的权值,m次操作,操作为1时为询问,每次询问给出 l 和 r ,求 f(l,r).操作为0时为修改权值.f(l,r)=f(l,l)⊕f(l,l+1)⊕⋯⊕f(l,r)⊕f(l+1,l+ ...

  9. 【noi 2.6_9277】Logs Stacking堆木头(DP)

    题意:给出在最底层的木头的个数,问有多少种堆放木头的方式.要求木头必须互相挨着在一起. 解法:f[i]表示最底层i个木头的堆放木头的方式.注意递推的思想!只需知道上一层堆放0~i-1个(即最底层堆放i ...

  10. AtCoder Beginner Contest 183 E - Queen on Grid (DP)

    题意:有一个\(n\)x\(m\)的棋盘,你需要从\((1,1)\)走到\((n,m)\),每次可以向右,右下,下走任意个单位,\(.\)表示可以走,#表示一堵墙,不能通过,问从\((1,1)\)走\ ...