• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

file could not be deleted

 
Saurabh Pillai
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 3041
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can run Java 7 you can do:

This will throw an IOException if the deletion fails which will reveal the reason.
 
Saurabh Pillai
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 20990
31
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3041
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul++

Saurabh, I think Wouter meant Files.delete(Path)
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20514
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Put the try-finally inside the for-loop, so you'll close all the input streams
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic