Dart的路径操作

简介: <div class="markdown_views"><h2 id="路径连接">路径连接</h2><pre class="prettyprint"><code class=" hljs lasso"><span class="hljs-keyword">import</span> <span class="hljs-string">'package:path/path

路径连接

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

main(List<String> arguments) {
  //将现有的两个路径连接
  var newPath = path.join('/Users/shailen', 'dart/projects');
  print(newPath);
}

输出有个问题,因为不同平台的路径分隔符是不同的,这个问题后面会解决

这里写图片描述

解析路径

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

main(List<String> arguments) {
  print(path.split('/Users/shailen'));
  print(path.split(r'C:\tempdir\tmp.txt'));
}

输出效果如下

这里写图片描述

计算相对路径

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

main(List<String> arguments) {
  //从当前目录到系统临时目录的路径
  print(path.relative(Directory.systemTemp.path));

  //使用相对路径
  var path1 = 'docs/book.html';
  var path2 = 'articles/list';
  print(path.relative(path1, from: path2));
  print(path.relative(path2, from: path1));

  //使用绝对路径
  var samples = 'http://www.dartlang.org/samples';
  var docs = 'http://www.dartlang.org/docs';
  print(path.relative(samples, from: docs));
  print(path.relative(docs, from: samples));
}

输出效果如下

这里写图片描述

URI和路径的转换

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

main(List<String> arguments) {
  var uri = path.toUri('http://dartlang.org/samples');
  print(path.fromUri(uri));
}

输出效果如下

这里写图片描述

路径文件的信息

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

main(List<String> arguments) {
  new File('${Directory.systemTemp.path}/dir/myFile.txt').create(recursive: true)
    .then((file) {
      //获取完整文件名
      print(path.basename(file.path));
      //获取文件目录
      print(path.dirname(file.path));
      //获取文件名
      print(path.basenameWithoutExtension(file.path));
      //获取文件扩展名
      print(path.extension(file.path));
    });
}

输出效果如下

这里写图片描述

平台的路径分隔符

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

main(List<String> arguments) {
  //下面两种方法都能得到当前平台的路径分隔符
  print(Platform.pathSeparator);
  print(path.separator);
}

这个方法就能解决第一个例子的问题,输出如下

这里写图片描述

目录
相关文章
|
小程序
微信小程序 - wx.showModal 内容换行
微信小程序 - wx.showModal 内容换行
990 0
|
设计模式 JSON JavaScript
前端开发人员使用的顶级 Node.js 框架介绍
前端开发人员使用的顶级 Node.js 框架介绍
1911 153
前端开发人员使用的顶级 Node.js 框架介绍
|
存储 iOS开发 Perl
ios-解决报错-CocoaPods could not find compatible versions for pod “xxx“
ios-解决报错-CocoaPods could not find compatible versions for pod “xxx“
1450 2
|
Dart Android开发 iOS开发
Flutter 弃用 WillPopScope 使用 PopScope 替代方法
了解如何在 Flutter 3.16 中将弃用的 WillPopScope 替换为 PopScope,并学习如何升级您的 Flutter 应用程序。详细指南和最佳实践,帮助您顺利迁移和更新您的导航逻辑。
968 7
Flutter 弃用 WillPopScope 使用 PopScope 替代方法
|
监控 安全 Linux
在Linux中,如何配置VPN服务?
在Linux中,如何配置VPN服务?
|
小程序
【微信小程序-原生开发】富文本编辑器 editor 的使用教程
【微信小程序-原生开发】富文本编辑器 editor 的使用教程
1961 0
【微信小程序-原生开发】富文本编辑器 editor 的使用教程
|
移动开发 Dart 前端开发
深度分析:React Native、Flutter、UniApp、Taro、Vue的差异
深度分析:React Native、Flutter、UniApp、Taro、Vue的差异
1695 6
|
JavaScript 小程序 开发者
uni-app开发实战:利用Vue混入(mixin)实现微信小程序全局分享功能,一键发送给朋友、分享到朋友圈、复制链接
uni-app开发实战:利用Vue混入(mixin)实现微信小程序全局分享功能,一键发送给朋友、分享到朋友圈、复制链接
3853 0
|
JSON Java 数据格式
response.contenttype详解
response.contenttype详解
|
设计模式 监控 架构师
如何在项目中考虑非功能需求
软件非功能需求包括性能、可靠性、安全性、易用性、可维护性、可移植性、兼容性、可重用性、可扩展性和可观察性。质量属性分为开发期和运行期,如易理解性、可扩展性、可测试性等是开发期质量,性能、安全性、易用性等是运行期质量。评估方法有ATAM(架构评估技术)、ADMEMS矩阵方法、SAAM(软件架构分析法)和CBAM(成本效益分析法)。ATAM包括建立评估小组、获取架构信息、风险承担者观点和形成最终报告四个阶段。
1170 0