"Usage note: This method does not automatically escape characters that are illegal in URLs. It is recommended that new code convert an abstract pathname into a URL by first converting it into a URI, via the toURI method, and then converting the URI into a URL via the URI.toURL method."
Fine; this does in fact work (except for Windows UNC paths, reported separately), and achieve what File.toURL was supposed to achieve to begin with. So why is File.toURL not @deprecated? The above message is precisely what @deprecated is for, according to the Javadoc manual:
"Adds a comment indicating that this API should no longer be used (even though it may continue to work)."
New code should not use File.toURL for the documented reasons, though it continues to mostly work (i.e. as much as it ever did).
Why is this issue important? Because most developers do not read the Javadoc of every single method they call, especially if they have been calling it for a while, and are not aware of the problems with File.toURL, whereas a deprecation message is made visible to them by the compiler. For example: in developing the NetBeans IDE, we often use URLs to persist file names (for interoperability with JAR entries etc.). The aforementioned bugs in File.toURL do affect this usage sometimes, e.g. if the IDE's users have a ClearCase installation, which uses '#' in directory names (I think). To be safe, we have to use file.toURI() (perhaps followed by URI.toURL(), according to the situation). However many developers do not know this and inadvertently use File.toURL since it is more visible (and has been around longer); and the compiler does not give any hint that this is not advisable.
Note that I was inspired to enter this issue based on feedback from a developer not working on NetBeans who had had the same experience:
http://www.netbeans.org/servlets/ReadMsg?msgId=743795&listName=nbdev
###@###.### 10/14/04 21:34 GMT
- relates to
-
JDK-4273532 toURL() method in File class needs to escape excluded URL characters
-
- Closed
-
-
JDK-6243829 Use of File.toURL in sun/jvmstat/perfdata/monitor/PerfDataBufferImpl.java
-
- Closed
-
-
JDK-6243824 Use of File.toURL in com/sun/org/apache/xalan/internal/xsltc - toURL known to return invalid URLs
-
- Resolved
-
-
JDK-6243835 Use of File.toURL in plugin and webstart - toURL known to return invalid URLs
-
- Resolved
-
-
JDK-6243815 Use of File.toURL in com/sun/java/swing/plaf/gtk/Metacity.java - toURL known to return invalid URLs
-
- Closed
-
-
JDK-6243826 Use of File.toURL in com/sun/security/auth/PolicyFile.java - toURL known to return invalid URLs
-
- Closed
-
-
JDK-6243831 Use of File.toURL in sun/tools/jstat/Arguments.java - toURL known to return invalid URLs
-
- Closed
-
-
JDK-6243837 Use of File.toURL in javawebstart - toURL known to return invalid URLs
-
- Closed
-
-
JDK-6243843 Use of File.toURL in plugin - toURL known to return invalid URLs
-
- Closed
-