IllegalArgumentException:调用现有文件夹时,URI不是分层的

时间:2017-08-07 21:10:37

标签: java uri java.nio.file

我这样做:

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)
...

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)
...

"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驱动器也没有区别。

1 个答案:

答案 0 :(得分:3)

它应该是file:// not file: