2014-04-15 121 views
0

我正在尝试使用java中的URL读取文件。使用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 applied to (java.net.URL) 

任何想法或建议,我怎么能解决这个例外真糊涂。

谢谢!

+0

did you mean'getClass()。getResource(“TextFile.rtf”);'? –

+1

“File”类将“URI”而不是“URL”作为参数。 'getResource()'返回一个'URL'。 –

+0

将我的帖子更新为网址。 – user1993412

回答

0

尝试转换URLURI相当于:

FileHelper.read(new File(getClass.getResource("TextFile.rtf").toURI())) 

更多信息请参见URL.toURI()

0

您只需从URL获取URI即可。

URL url = getClass.getResource("TextFile.rtf"); 
URI uri = url.toURI(); 
FileHelper.read(new File(uri))