首页 > 解决方案 > 给定驱动器的工作目录是什么?

问题描述

我正在从“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是一个有效路径,它指向该驱动器上的工作目录。

标签: javaionio

解决方案


推荐阅读