tags:

views:

800

answers:

5

Why is the function java.io.File.toURL() deprecated? I need to pass an URL to Toolkit.createImage() which accepts a URL object. Javadoc recommends me to use File.toURI().toURL(). However:

C:\Documents and settings\Administrator\...

becomes:

C:\Documents%20and%20settings\Administrator\...

which obviously is an invalid file location. I've found File.toURL() to create URL's without the escaping, however it's deprecated. Although it works I'm scared of using deprecated functions. What's a method that's not deprecated that does the same thing?

EDIT: Right now my code looks like:

spriteImage1 = tkit.createImage(new File("./images/sprite1.png").getCanonicalFile().toURL());

EDIT: I need to create an Image from a folder outside my .jar file. I'll need a relative location ("./images/sprite1.png"). The method createImage(String) throws an exception when I try to give it the relative path.

+10  A: 

It is known to not handle special characters correctly.

You might want to read this page.

Lucas McCoy
+10  A: 

Wouldn't it be easier to use Toolkit.createImage(File.getPath()); instead?

R. Bemrose
+5  A: 

Why don't you just use the createImage function that takes a String filename instead?

Pesto
+1  A: 

You could consider using ImageIO as an alternative.

McDowell
A: 

Better. Since none of the answers here worked, I found out how to disable the Eclipse warning for deprecation. Thanks all!

unknown
Disabling the warning on a deprecated method does not eliminate the fact that the method is deprecated.
Martin OConnor