java - 给定驱动器的工作目录是什么?
问题描述
我正在从“E”驱动器在 Windows 操作系统 Eclise 上运行以下代码。我得到一个我不明白的错误。
System.out.println("Java version:"+Runtime.version());
try (DirectoryStream<Path> ds = Files.newDirectoryStream(Path.of("e:"))){
for(Path p : ds) {
System.out.println("root:" + p.getRoot() + " nameCount:"
+ p.getNameCount() + " " + p);
}
}
Path file = Path.of("e:pom.xml");
System.out.println("\n\nfileSize:" + Files.size(file) + " "+file.toAbsolutePath());
try {
file = Path.of("d:pom.xml").toAbsolutePath();
Files.size(file);
System.out.println(file.toString());
}catch(Exception e ) {
System.out.println(e.toString());
}
try {
file = Path.of("z:pom.xml").toAbsolutePath();
}catch(Throwable e ) {
System.out.println(e.getCause().toString());
}
输出:
Java version:15.0.1+9-18
root:e: nameCount:1 e:.classpath
root:e: nameCount:1 e:.project
root:e: nameCount:1 e:.settings
root:e: nameCount:1 e:bin
root:e: nameCount:1 e:out
root:e: nameCount:1 e:pom.xml
root:e: nameCount:1 e:src
root:e: nameCount:1 e:target
fileSize:1276 E:\workspace\Java\Temp\pom.xml
java.nio.file.NoSuchFileException: D:\pom.xml
java.io.IOException: Unable to get working directory of drive 'Z'
查看输出e:pom.xml
是一个有效路径,它指向该驱动器上的工作目录。
e:pom.xml
不一样e:\pom.xml
。为什么它是有效路径?这是什么意思?- 为什么它在驱动器“d”上找到工作目录作为驱动器根本身?由于 CWD 在驱动器 'e' 上,它应该为路径 'd:pom.xml' 抛出 java.nio.file.InvalidFileException
- 如果执行时只有一个 CWD(当前工作目录),为什么会说“无法获取其他驱动器的工作目录”?我可以为每个可用驱动器设置单独的工作目录吗?
解决方案
推荐阅读
- arrays - 如何使用 Node js 将孩子添加到嵌套的孩子?
- python - 如果行包含 Pandas 中的特定值,则删除列
- swift - Swift - Bond Framework - 一对多绑定关系
- asp.net-core - 使用 Tag Helpers 从 Razor 页面上的表单绑定模型失败
- c# - 是否有更好的 LinkedList 线程安全实现
- node.js - Nodejs 将图像数据转换回实际图像
- javascript - 当包括胜利图时,反应原生滑块变得迟钝并且状态值变化最近反映
- java - NullPointerException 使用 Java 中的库从 GCS 读取文件
- python - 是否可以使用 bs4 将新实例添加到一行?
- javascript - 在较小的可见容器内移动大图像