Dart: path库
安装:
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库的更多相关文章
- Dart http库
推荐下我写的一个http库ajanuw_http 最基本的获取数据 import 'package:http/http.dart' as http; main(List<String> a ...
- Dart自定义库、系统库和第三方库
/* 前面介绍Dart基础知识的时候基本上都是在一个文件里面编写Dart代码的,但实际开发中不可能这么写,模块化很重要,所以这就需要使用到库的概念. 在Dart中,库的使用时通过import关键字引入 ...
- flutter-web利用dart js 库发起http request
初学flutter,初学前端,尝试在dart中直接使用HttpClient时,直接报出Platform not supported,查资料发现他还不支持浏览器. 通过查阅资料发现可以借助axios 与 ...
- Dart: puppeteer库
和node的差不多,只有写API不一样 puppeteer 地址 安装依赖 dependencies: puppeteer: ^1.7.1 下载 chrome-win 到 <project_ro ...
- Atitit.dart语言的特性 编译时js语言大总结
Atitit.dart语言的特性 编译时js语言大总结 1. 原型环境1 1.1. Dart可以编译js3 2. 第二个期待的理由是Dart的语言特性,没有什么特别特性好像,类似java c#一小时 ...
- Dart学习笔记
一.数据类型 1. 字符串 和 数字 互转 // String 转为 int '); assert(one == ); // String 转为 double var onePointOne = do ...
- 【dart学习】-- Dart之异步编程
一,概述 编程中的代码执行,通常分为同步与异步两种. 同步:简单说,同步就是按照代码的编写顺序,从上到下依次执行,这也是最简单的我们最常接触的一种形式.但是同步代码的缺点也显而易见,如果其中某一行或几 ...
- Angular JS | Closure | Google Web Toolkit | Dart | Polymer 概要汇集
AngularJS | Closure | Google Web Toolkit | Dart | Polymer GWT https://code.google.com/p/google-web-t ...
- [dart学习]第三篇:dart变量介绍 (二)
本篇继续介绍dart变量类型,可参考前文:第二篇:dart变量介绍 (一) (一)final和const类型 如果你不打算修改一个变量的值,那么就把它定义为final或const类型.其中:final ...
随机推荐
- Jenkins入门教程
Jenkins入门教程 @ 目录 Jenkins入门教程 1. 什么是Jenkins 1.1 我们为啥需要jenkins 1.2. Jenkin实现原理 2. Jenkins搭建 2.1. Jenki ...
- 将Oracle数据,以及表结构如何传输至MySQL
最近研究数据库,将Oracle数据库中的表结构以及数据传输给MySQL数据库,自己通过学习采用两种方式,效率较高. 方式一:Navicat 自从下载了Navicat,真的发现这是一款操作数据库十分优秀 ...
- Linux环境ZooKeeper安装配置及使用
Linux环境ZooKeeper安装配置及使用 一.ZooKeeper 1.1 zookeeper作用 1.2 zookeeper角色 1.3 zookeeper功能 二.集群规划 三.安装流程 (1 ...
- MariaDB数据库----查(实例演示)
MariaDB数据--查 SQl语句执行顺序 基础查询 查询 添加数据 MariaDB [test]> insert into huluwa values -> (1 ,'葫芦爷爷',73 ...
- Leetcode LRU缓存,数组+结构体实现
一.算法思路 LRUCache类有以下函数和变量: LRUCache(int capacity): capacity是当前对象能够存储的键值对(key,value)最大个数. int get(int ...
- C语言之数据在内存中的存储
C语言之数据在内存中的存储 在我们学习此之前,我们先来回忆一下C语言中都有哪些数据类型呢? 首先我们来看看C语言中的基本的内置类型: char //字符数据类型 short //短整型 int //整 ...
- C++ 标准模板库(STL):vector
目录 1. vector 1.1 vector的定义 1.2 vector容器内元素的访问 1.3 vector 常用函数实例解析 1.4 vector的常见用途 1. vector 变长数组,长度根 ...
- 昨晚12点,女朋友突然问我:你会RabbitMQ吗?我竟然愣住了。
01为什么要用消息队列? 1.1 同步调用和异步调用 在说起消息队列之前,必须要先说一下同步调用和异步调用. 同步调用:A服务去调用B服务,需要一直等着B服务,直到B服务执行完毕并把执行结果返回给A之 ...
- idea使用maven的打包工具package不会打上主类解决方法
- mysql创建和使用数据库
mysql连接和断开 mysql -h host -u user -p******** /*建议不要在命令行中输入密码,因为这样做会使其暴露给在您的计算机上登录的其他用户窥探*/ mysql -u u ...