[ACCEPTED]-File.toURL() deprecated?-io

Accepted answer
Score: 38


URL url2 = file.toURI().toURL();


Score: 14

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

Score: 11

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


Score: 5

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

Score: 5

I think the right answer to this question 5 is the one exposed in this link http://www.jguru.com/faq/view.jsp?EID=1280051

As exposed 4 in the link above, the right way to do this 3 is:

// Supposing f is the referenced file in code and we want to
// get an URL instance without deprecation warnings.
URL externalForm = f.toURI().toURL();

There is no catch for MalformedURLException 2 as the question is about deprecation of 1 the method file.toURL().

Hopes this helps.

Score: 1

You could consider using ImageIO as an alternative.


Score: 0

i think that you must write the url in this 2 way

C:/Documents and settings/Administrator/...

not 1 in this

C:\Documents and settings\Administrator...

More Related questions