3 Replies Latest reply: May 29, 2012 1:08 AM by Sreram RSS

    Deleting a Gif image

    Sreram
      I have a strange problem. I deal with different files of different formats including, jpg, bmp, png, zip etc. I am able to successfully create it and do the necessary processing of those files.

      But, when i try to delete the files, am not able to delete specifically gif files, while i am able to successfully delete other files. I don't have any security manager, and no security exception is raised. Yet file.delete() returns me false [I use Java 6 for your info]. i have this problem only with gif files. Any ideas?

      Sreram
        • 1. Re: Deleting a Gif image
          sabre150
          If you are absolutely sure that the file is closed and you are absolutely sure that no other process anywhere on your computer is using the file then just before you try to delete the file use :-
          System.gc();
          I know this sounds silly and I can't guarantee it will work but give it a try - what have you got to lose?
          • 2. Re: Deleting a Gif image
            Sreram
            Thanks for your suggestion of System.gc(). But that didn't work.

            As you had also suggested 'you are absolutely sure that no other process anywhere on your computer' I wanted to make myself 100% sure that the file is not at all being used even in my own application, so i created a new file reference to the file, and then tried deleting it, and hurray! file got deleted.

            My old code which didn't delete the file.
            File file = getFile(); //Here i get the file reference which i use everywhere in my code
            if (file != null && file.exists()) {
                 file.delete();
            }
            Code change which made the trick.
            File file = new File(getFolderPath() + File.separator + getFile().getName());
            if (file != null && file.exists()) {
                 file.delete();
            }
            Thank you sabre150.

            Sreram
            • 3. Re: Deleting a Gif image
              Sreram
              And more over what i found out is the file is being used if i use a Toolkit.getDefaultToolkit().createImage(filename) to get the image directly from the image file. If i read the file in a FileInputStream, properly close the stream and give the data to the toolkit to create the image - Toolkit.getDefaultToolkit().createImage(byteData), this problem never occurs.

              Is the stream which is used to read the file in Toolkit.getDefaultToolkit().createImage(filename) properly closed? I am not sure. But hope this helps someone.

              Sreram