dart recipe file.rename()重命名文件,但不更新对象中的路径
dart cookbook配方“重命名文件、目录或符号链接”似乎无法按预期工作:dart recipe file.rename()重命名文件,但不更新对象中的路径,dart,Dart,dart cookbook配方“重命名文件、目录或符号链接”似乎无法按预期工作: import 'dart:io'; main() async { // Get the system temp directory. var systemTempDir = Directory.systemTemp; // Create a file. var file = await new File('${systemTempDir.path}\\foo.txt').create();
import 'dart:io';
main() async {
// Get the system temp directory.
var systemTempDir = Directory.systemTemp;
// Create a file.
var file = await new File('${systemTempDir.path}\\foo.txt').create();
// Prints path ending with `foo.txt`.
print('The path is ${file.path}');
// Rename the file.
await file.rename('${systemTempDir.path}\\bar.txt');
// Prints path ending with `bar.txt`.
print('The path is ${file.path}');
}
输出显示文件对象的内部路径字段未更改(尽管重命名成功):
我扩展/修改了烹饪书示例,以进一步研究这一点:
import 'dart:io';
main() async {
var systemTempDir = Directory.systemTemp;
var file = await new File('${systemTempDir.path}\\foo.txt').create();
print('The file is located at ${file.path}');
File toDelete;
var newName = '${systemTempDir.path}\\fubar.toodeloo';
await file.rename(newName);
if (await new File(newName).exists() == false) {
print('The rename failed: there is no ${newName} file');
} else {
var newFile = new File(newName);
print('The rename was succesful');
var nameChangedInObject = file.path == newName;
if (nameChangedInObject) {
print('The path of the file object has changed correctly');
toDelete = newFile;
} else {
print(
'The path in the file object still is: ${file.path}');
toDelete = newFile;
}
await toDelete.delete();
print(
'And now, ${toDelete.path} is gone: ${await toDelete.exists() == false}');
}
}
此输出确认内部路径字段未使用新名称更新的事实:
[Running] dart "d:\src\dart\renamingExample.dart"
The file is located at C:\Users\guivh\AppData\Local\Temp\foo.txt
The rename was succesful
The path in the file object still is: C:\Users\guivh\AppData\Local\Temp\foo.txt
And now, C:\Users\guivh\AppData\Local\Temp\fubar.toodeloo is gone: true
[Done] exited with code=0 in 0.369 seconds
我正在windows机箱上运行开发版本:
PS D:\src\dart> dart --version
Dart VM version: 2.0.0-dev.39.0 (Fri Mar 16 00:17:07 2018 +0100) on "windows_x64"
PS D:\src\dart>
有人能解释一下这里发生了什么吗?这个例子是错误的。
文件
对象,因此它肯定不会被重命名操作更改。这个例子的预期是错误的。显然你是对的。但是官方的dart-by-example页面出错了吗?
PS D:\src\dart> dart --version
Dart VM version: 2.0.0-dev.39.0 (Fri Mar 16 00:17:07 2018 +0100) on "windows_x64"
PS D:\src\dart>