Java IO FileNotFoundException after converting fil

2019-09-06 11:21发布

问题:

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/...".



标签: java jython