java URI路径不是绝对异常java(不是android)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32780096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:45:49  来源:igfitidea点击:

URI path not absolute exception java(not android)

javafileexceptionpathuri

提问by Jürgen K.

I have the following piece of source code

我有以下源代码

import java.net.URI;
import java.net.URISyntaxException;

    File file = new File("pic.png");
    BufferedImage image = ImageIO.read(file);
    String string = "pic.png";
 //the code works fine until here
    URI path = new URI(string);
    File f = new File(path);
    ColorProcessor image = new ColorProcessor(ImageIO.read(f));

So the path that the File gets is correct. Image is buffered correctly also. Now my problem is that i'm getting the following exception

所以File获取的路径是正确的。图像也被正确缓冲。现在我的问题是我收到以下异常

Exception in thread "main" java.lang.IllegalArgumentException: URI is not absolute
    at java.io.File.<init>(Unknown Source)

Why is my path not absolute?And how do i do it right?

为什么我的路径不是绝对的?我该怎么做?

If i change the path like this:

如果我像这样改变路径:

String string = "C:'\'Users'\'Jurgen'\'newFile'\'myProject'\'pic.png";

Also tried like this

也试过这样

String string = "C:/Users/Jurgen/newFile/myProject/pic.png";

Then i get a new exception

然后我得到一个新的例外

Exception in thread "main" java.lang.IllegalArgumentException: URI is not hierarchical
    at java.io.File.<init>(Unknown Source)

P.S. not working with android packages for uri

PS 不适用于 uri 的 android 包

Thanks in advance=)

提前致谢=)

采纳答案by Stanislav

You are trying to create uniform resource identifier, but the name should follow the url conventions. This means, that it's necessary to provide a scheme (take a look here, to see all available schemes). So, in your case, you have to create the URI with string, like file:/pic.pngor may be some other scheme.

您正在尝试创建统一资源标识符,但名称应遵循url 约定。这意味着,有必要提供一个方案(看这里,查看所有可用的方案)。因此,在您的情况下,您必须使用字符串创建 URI,例如file:/pic.png或可能是其他一些方案。

As for your full path, it could be done like:

至于您的完整路径,可以这样做:

String string = "file:/C:/Users/Jurgen/newFile/myProject/pic.png";