我发现无法简单地获取文件名 :(
Dart代码:
File file = new File("/dev/dart/work/hello/app.dart");
如何获取文件名 app.dart
?
我没有找到相关的API,所以我会这样做:
var path = file.path;
var filename = path.split("/").last;
有更简单的解决方案吗?
您可以使用path包:
import 'dart:io';
import 'package:path/path.dart';
main() {
File file = new File("/dev/dart/work/hello/app.dart");
String filename = basename(file.path);
}
如果您不想使用路径pub包,可以使用来自dart:io的Uri类,该类返回文件名及其扩展名:
如果您不想使用路径pub包,可以使用来自dart:io的Uri类,该类返回文件名及其扩展名:String fileName = File(localFilePath).uri.pathSegments.last;
Uri
对象,这将非常有用。 - Chuck Batson2.6
宣布并且可用于Flutter版本 1.12
及以上,您可以使用 extension
方法。它将为此问题提供更易读和全局的解决方案。
file_extensions.dart
:import 'dart:io';
extension FileExtention on FileSystemEntity{
String get name {
return this?.path?.split("/")?.last;
}
}
现在所有文件对象都新增了name
获取器。您可以在任何文件上调用name
以获取文件名。
main() {
File file = new File("/dev/dart/work/hello/app.dart");
print(file.name);
}
阅读文档以获取更多信息。
注意:由于extension
是一项新功能,它尚未完全整合到IDE中,并且可能无法自动识别。您需要在需要使用的位置手动导入您的extension
。只需确保已导入扩展文件:
import 'package:<your_extention_path>/file_extentions.dart';
"/"
路径分隔符,而应该使用 Platform.pathSeparator
。 - rlat适用于2020年4月
void main() {
var completePath = "/dev/dart/work/hello/app.dart";
var fileName = (completePath.split('/').last);
var filePath = completePath.replaceAll("/$fileName", '');
print(fileName);
print(filePath);
}
"/"
路径分隔符,应该使用 Platform.pathSeparator
来代替。 - rlatPath
对象,使用它的filename
属性获取文件名。import 'dart:io';
void main() {
var file = new File('/dev/dart/work/hello/app.dart');
Path path = new Path(file.path);
print(path.filename); // 'app.dart'
print(path.directoryPath); // '/dev/dart/work/hello'
}
Path
已经被弃用,将在8月11日被移除。” - 详见http://dartbug.com/11666 - Alexandre ArdhuinString url= https://s3tytf.eu-central-1.wasabisys.com/case-ordersd/2/11/1/215/ODHC01jghf0113592015/oVz058q2ZLjkjghvDVEITM0Vw1G8wexkjI1ENkmZOehgsYNMy1idFYG6otpZoDJKjZU2s.pdf?AWSAccessKeyId=0VNN4TZAGM41ULSF&Signature=Wev4LVOoqjLd1W8I1XBdOnE%3D&Expires=1632993368876
String pdfName = Uri.parse(url).path.split("/").last
补充@Alexandre Ardhuin的回答。 path库的文档:
/// Gets the part of [path] after the last separator.
///
/// p.basename('path/to/foo.dart'); // -> 'foo.dart'
/// p.basename('path/to'); // -> 'to'
///
/// Trailing separators are ignored.
///
/// p.basename('path/to/'); // -> 'to'
String basename(String path) => context.basename(path);
/// Gets the part of [path] after the last separator, and without any trailing
/// file extension.
///
/// p.basenameWithoutExtension('path/to/foo.dart'); // -> 'foo'
///
/// Trailing separators are ignored.
///
/// p.basenameWithoutExtension('path/to/foo.dart/'); // -> 'foo'
String basenameWithoutExtension(String path) =>
context.basenameWithoutExtension(path);
那么,让我们尝试使用basenameWithoutExtension
函数
import 'dart:convert';
import 'dart:io';
void main() async {
assetsGenerator();
}
void assetsGenerator({String assetsDir='/assets/images/'}) async {
var assetsPaths = Directory(Directory.current.path + assetsDir);
try {
var assetsList = assetsPaths.list();
await assetsList.forEach((element) {
String imageName = element.path.split('/').last;
String imagePath = assetsDir.split('/')[1] +
'/' +
assetsDir.split('/')[2] +
'/' +
imageName;
print(imagePath);
});
} catch (error) {
print(error.toString());
}
}
结果
assets/images/pexels-photo-2102416.jpeg
assets/images/pexels-photo.jpeg
assets/images/pexels-photo-7988087.jpeg
assets/images/pexels-photo-5052877.jpeg
assets/images/pexels-photo-326501.jpeg
assets/images/imac-apple-mockup-app-38544.jpeg
获取图像名称并将其存储在imageName
变量中
imac-apple-mockup-app-38544.jpeg
File
应该有一个.name
的 API。 - Freewind