Java IO FileNotFoundException after converting file name toURL()

不羁的心 提交于 2019-12-25 01:40:25

问题


Here is the Jython code (although this may not be a Jython-specific issue)...

file_name = "Manifest.ttl"
file_url = File(file_name).toURL()
f = File(file_url.toString())

java.io.FileNotFoundException: java.io.FileNotFoundException: file:/home/james/projects/wordnet/wordnet30/rdf/Manifest.ttl (No such file or directory)


回答1:


Javadoc to the rescue:

Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname.

Parameters: pathname - A pathname string

The File constructor takes an abstract path name as argument, not the toString representation of a URL.

Besides, toURL is deprecated. You might use toURI, and reconstruct the file with this URI.




回答2:


toURL() adds the file:// prefix that a proper URL/URI requires. Clearly the File constructor doesn't check and remove this prefix, so it's looking for a file called "file://..." instead of where you want it to look "/home/james/...".



来源:https://stackoverflow.com/questions/7247427/java-io-filenotfoundexception-after-converting-file-name-tourl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!