Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File.toURL() deprecated?

Tags:

java

url

io

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.

like image 329
Lucky Avatar asked Apr 22 '09 14:04

Lucky


4 Answers

Use:

URL url2 = file.toURI().toURL();
like image 67
ks. Avatar answered Oct 08 '22 04:10

ks.


It does not handle special characters correctly as per this bug.

like image 37
Kredns Avatar answered Oct 08 '22 05:10

Kredns


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

like image 43
Powerlord Avatar answered Oct 08 '22 03:10

Powerlord


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

like image 24
Pesto Avatar answered Oct 08 '22 04:10

Pesto