参数异常:调用现有文件夹时URI不是分层结构

3
我正在做这件事情:
private boolean createCopy(String targetDirectory, String[] dataSet, String fileName, boolean overwrite) throws IOException, URISyntaxException
{
    fileName = "file:" + fileName.replace(" ","%20");
    URI uri = new URI("file:" + targetDirectory);
    Path dPath = Paths.get(uri);
    //code
 }

我遇到了这个异常:

Exception in thread "main" java.lang.IllegalArgumentException: URI is not hierarchical
at sun.nio.fs.WindowsUriSupport.fromUri(WindowsUriSupport.java:122)
at sun.nio.fs.WindowsFileSystemProvider.getPath(WindowsFileSystemProvider.java:92)
at java.nio.file.Paths.get(Paths.java:138)
...

At the

Path dPath = Paths.get(uri);

有人知道为什么会出现这种情况吗?targetDirectory只是一个简单的文件夹,既不是JAR文件也不是WAR文件;如果我去掉

URI uri = new URI("file:" + targetDirectory);

我得到了

Exception in thread "main" java.nio.file.FileSystemNotFoundException: Provider "DRIVE_LETTER" not installed
at java.nio.file.Paths.get(Paths.java:147)
...
< p >"DRIVE_LETTER" 最终会变成像 "C"、"D" 或 "E" 这样的东西。它是 targetDirectory 所在的驱动器。

编辑:

public static void main(String... args)
{
    Path path = null;
    try
    {
        Paths.get(new URI("file:E://HTML%20Processor//test//copies//"));
    }
    catch (URISyntaxException e)
    {
        e.printStackTrace();
    }
}

抛出完全相同的异常,因为您想知道确切的调用。

编辑:将文件放在任何其他驱动器上都没有区别; USB或SATA驱动器也没有区别。


那么,您能友善地提供给我们调用此方法的代码吗? - Anas
你能准确地告诉我你的目标目录路径吗?如果可能的话,请提供完整的方法和传递给它的值。java.nio.file.FileSystemNotFoundException 表示驱动器不可访问。请问你在运行此代码的机器上是否挂载了指定的驱动器路径? - Acewin
提供了一个简单的方法,会抛出相同的错误! - Chaoscrasher
驱动器位置像往常一样可以访问;我可以移动文件、复制、删除。系统驱动器与否并没有任何区别。 - Chaoscrasher
1个回答

3

应该使用file://而不是file:


3
实际上,file://会导致其他错误。对我来说,file:/是解决方案。 - Chaoscrasher

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接