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.
Use:
URL url2 = file.toURI().toURL();
It does not handle special characters correctly as per this bug.
Wouldn't it be easier to use Toolkit.createImage(File.getPath());
instead?
Why don't you just use the createImage
function that takes a String filename instead?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With