wood burning stoves*
The moose likes Java in General and the fly likes file could not be deleted Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "file could not be deleted" Watch "file could not be deleted" New topic
Author

file could not be deleted

Saurabh Pillai
Ranch Hand

Joined: Sep 12, 2008
Posts: 506


Above is a code snippet of my file parsing code. Please consider following points.

- It reads files from specific location from my local, it saves the file into byte[] and save it into database and at last delete the file.
- I tested it with one file and it works like a charm.
- File parser implements Timetask class. It runs every minute.
- This time I kept 10 files in that directory. I checked the log and found following
     - It processes all 10 files and saves them into db but delete only one file out of 10 files.
     - file parser will run again after a minute and again parses remaining files and delete only 3-4 files out of remaining files.
     - In 3rd or 4th parsing cycle it is able to delete all 10 files.

why does it work this way? does it have anything to do with synchronization? As it can delete file it confirms there is no file permission issue.

Thank you.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2991
    
    9
Well, there are a number of possible reasons for delete() to return true - but from your description, I think the most likely scenario is that something is still reading or writing those files at the time you first try to delete them. So first: are you sure that whatever you're doing to these files is really complete? It's possible you're simply trying to delete the files too early. Second, did you remember to close all streams or RandomAccessFile objects as soon as you were done with them? The behavior you describe could be because there's a stream object that hasn't been closed yet, and hasn't been garbage collected. Make sure all streams and other Closeable objects are closed in a finally block, to make sure it happens even if an exception is thrown.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

If you can run Java 7 you can do:

This will throw an IOException if the deletion fails which will reveal the reason.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Saurabh Pillai
Ranch Hand

Joined: Sep 12, 2008
Posts: 506
Mike Simmons wrote:Well, there are a number of possible reasons for delete() to return true - but from your description, I think the most likely scenario is that something is still reading or writing those files at the time you first try to delete them. So first: are you sure that whatever you're doing to these files is really complete? It's possible you're simply trying to delete the files too early. Second, did you remember to close all streams or RandomAccessFile objects as soon as you were done with them? The behavior you describe could be because there's a stream object that hasn't been closed yet, and hasn't been garbage collected. Make sure all streams and other Closeable objects are closed in a finally block, to make sure it happens even if an exception is thrown.


I checked my code again and only one place I am using two streams (FileInputStream and ByteArrayOutputstreams) and I am closing both of them in finally block. Let me give you overview the way I have implemented this.

1) First look at specific location if new files are available,


2) Load the File object in another class ImageDataBlock,



ImageDataBlock.java



3) Parsing

Saurabh Pillai
Ranch Hand

Joined: Sep 12, 2008
Posts: 506
Wouter Oet wrote:If you can run Java 7 you can do:

This will throw an IOException if the deletion fails which will reveal the reason.


Hello Wouter,

I have downloaded JDK 1.7.0. Path is interface and it does not have delete method. Am I looking at wrong place. There is one class but it is Paths with s at the end.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

In your code identified with the number (2) you are reading any number of files but you're only closing the last one which you read.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2991
    
    9
Paul++

Saurabh, I think Wouter meant Files.delete(Path)
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

I'm really confused right now because my IDE accepted my code and just saw 2 different versions of the Path class (an interface and an abstract class). Can't seem to view them now. I'll keep searching but @Mike, you're right.
Saurabh Pillai
Ranch Hand

Joined: Sep 12, 2008
Posts: 506
Indeed, unclosed streams was the issue. I just tested it and it works perfectly. Thank you Mike, Wouter and Paul for your help.

Do you guys have any comments on my code for improvement?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

Put the try-finally inside the for-loop, so you'll close all the input streams


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: file could not be deleted
 
Similar Threads
Ant javac task and subdirectories
Passed OCMJD.
parent child relation ship
Deleting while using timer
workflow implemetation with stateful session bean