How to close ImageIcon file

P

Phil...

I have a directory that has lots of jpg and gif files.
I want to delete all of the small ones, those with
height or width less than 100 px.
I use close to the following code to get the height and width.

File dir = new File(".");
String files[] = dir.list();
ImageIcon myimage = new ImageIcon(files);
int height = myimage.getIconHeight();
int width = myimage.getIconWidth();

At this point, File.delete() returns false
most of the time. My guess is that the file
is still open. How can I close it so I can delete it?
N.B. File.deleteOnExit() works fine.

Tnx, phil...
 
W

William Brogden

I have a directory that has lots of jpg and gif files.
I want to delete all of the small ones, those with
height or width less than 100 px.
I use close to the following code to get the height and width.

File dir = new File(".");
String files[] = dir.list();
ImageIcon myimage = new ImageIcon(files);
int height = myimage.getIconHeight();
int width = myimage.getIconWidth();

At this point, File.delete() returns false
most of the time. My guess is that the file
is still open. How can I close it so I can delete it?
N.B. File.deleteOnExit() works fine.

When you load an image that way, a separate Thread is started to load the file.
Your result varies because sometimes the file is still being loaded.
Look at the JavaDocs for ImageIcon - the getImageLoadStatus in particular.
You can't reliably look at the height and width before the image is loaded.

Bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top