【问题标题】:IllegalArgumentException: URI is not hierarchical when calling existing folderIllegalArgumentException:调用现有文件夹时 URI 不是分层的
【发布时间】:2017-08-07 21:10:36
【问题描述】:

我正在这样做:

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

【问题讨论】:

  • 那么,您能否向我们提供对这个方法的调用?
  • 你能准确说出你的目标目录路径是什么吗?如果可能,还提供完整的方法。和传递给它的值。 java.nio.file.FileSystemNotFoundException 表示驱动器不可访问。您是否在运行此代码的机器上安装了指定的驱动器路径。
  • 提供了一个简单的方法,抛出同样的错误!
  • 像往常一样可以访问驱动器位置;我可以移动文件,复制,删除。系统驱动与否也没有区别。

标签: java uri java.nio.file


【解决方案1】:

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

【讨论】:

  • 其实file://会导致其他错误。 file:/ 是我的解决方案。
猜你喜欢
  • 2014-08-16
  • 1970-01-01
  • 2013-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-26
  • 2019-01-13
  • 1970-01-01
相关资源
最近更新 更多