Java 使用URL读取文件
我正在尝试使用java中的URL读取文件Java 使用URL读取文件,java,java-6,Java,Java 6,我正在尝试使用java中的URL读取文件 FileHelper.read(new File(getClass.getResource("TextFile.rtf"))) 我真的对下面的例外感到困惑 error: overloaded method constructor File with alternatives: (java.net.URI)java.io.File <and> (java.lang.String)java.io.File cannot be app
FileHelper.read(new File(getClass.getResource("TextFile.rtf")))
我真的对下面的例外感到困惑
error: overloaded method constructor File with alternatives:
(java.net.URI)java.io.File <and>
(java.lang.String)java.io.File
cannot be applied to (java.net.URL)
错误:重载了方法构造函数文件,其中包含替代项:
(java.net.URI)java.io.File
(java.lang.String)java.io.File
无法应用于(java.net.URL)
任何想法或建议如何解决此异常
谢谢 尝试将
URL
转换为其URI
等效项:
FileHelper.read(new File(getClass.getResource("TextFile.rtf").toURI()))
有关更多信息,请参阅。您需要做的就是从URL获取URI
URL url = getClass.getResource("TextFile.rtf");
URI uri = url.toURI();
FileHelper.read(new File(uri))
你是说
getClass().getResource(“TextFile.rtf”)
?文件
类将URI
而不是URL
作为参数getResource()
返回一个URL
。将我的帖子更新为URL。