Java文件和文件类都无法识别UNC目录

时间:2014-05-15 14:51:01

标签: java

我已经基于UNC路径在Java中创建了File和Files对象。当我检查对象是否是目录时,两个类都返回false:

        sourceDirectory = new File( "\\\\mymachine\\test\\new\\" );
        boolean b = sourceDirectory.isDirectory();
        Path path = sourceDirectory.toPath();
        boolean a = Files.isDirectory( path );

        results:  b=false and a=false

我需要做什么才能通过文件和文件将我的UNC目录识别为目录?

1 个答案:

答案 0 :(得分:1)

其实你这样做是正确的,但你当地的测试案例会误导你 对于网络共享上的Windows路径,您的代码可以正常工作,但对于本地的 您的计算机上的路径,您仍然需要指定驱动器号:

    File netDir = new File("\\\\usstll0032\\share\\drc"); // network drive
    System.out.println(netDir.isDirectory()); // true

    File badDir = new File("\\\\us39-0cmq142\\temp"); // my computer
    System.out.println(badDir.isDirectory());  // false

    File goodDir = new File("\\\\us39-0cmq142\\c$\\temp"); // my computer
    System.out.println(goodDir.isDirectory()); // true