我已查看此问题What does 'URI has an authority component' mean?
中提供的解决方案在提出这个问题之前,我在Intellij
遇到了同样的问题。我拥有的是
源代码:
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
public class FileLoading {
public static void main(String ... args){
File someFile = null;
try {
someFile = new File( new URI("file://somefile.txt"));
} catch (URISyntaxException e) {
e.printStackTrace();
}
System.out.println(someFile.getPath());
}
}
错误:
"C:\Program Files\Java\jdk1.8.0_162\bin\java" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2017.3.4\lib\idea_rt.jar=51711:C:\Program Files\JetBrains\IntelliJ IDEA 2017.3.4\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_162\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_162\jre\lib\rt.jar;C:\Users\U631156\code\java\target\classes" FileLoading
Exception in thread "main" java.lang.IllegalArgumentException: URI has an authority component
at java.io.File.<init>(File.java:423)
at FileLoading.main(FileLoading.java:9)
Process finished with exit code 1
我该如何解决这个问题。
答案 0 :(得分:2)
如果您没有为文件指定主机,则语法应为file:///somefile.txt
(注意3斜杠)。
相关的RFC RFC 8089在第2部分中显示了URI的结构,它将2个斜杠之后的部分标识为auth-path
(因此您的错误)。
但请注意,这会将“somefile.txt”放在文件系统的根目录中,因为URI没有目录路径。