views:

86

answers:

3

Hey guys,

My problem is that I have an app which is writing a lot of relatively (100-500kb) small CSV files (tens and hundreds of thousands ). Content of those files then get loaded in database via sql loader call (its oracle db) and this is what I have to live with.

So, I need to remove those small files time to time to prevent them from eating up all space. I would like to attach that to the activity which writes those files and loads them into db as a last finalize step.

My Question is -- how in java can one remove a bunch of small files with less overhead on performance?

Thanks in advance! Michael

+4  A: 

Well, file.delete() should suffice (it is internally implemented as a native method)

Bozho
additionaly the method delete() returns a boolean value if the file or directory is successfully deleted.
Agusti-N
I hope, but anyway will make a prototype and try out the timings.
Zorkus
You might also look into deleting a full directory with the method delete(). If this works, it may have some optimizations that make it much faster, but I tend to believe it will just fail.
Bill K
+1  A: 

You may find it an order of magnitude faster if you shell out and have the system delete them. You'd have to be able to hit a stopping point (where no files were being processed) then shell out and delete "*" or . or whatever it is for your OS.

(Note, this makes your program VERY os dependent!)

Be sure on Windows and Mac that you are bypassing the trashcan feature!

The nice thing about del . or rm * is that they SHOULD batch the operation rather than repeatedly opening, modifying and closing the directory.

You might also write filenames with a pattern like a001, a002, a003, ... and when you reach a999 you go to b001 and delete a*.

Bill K
Thats right too. I will thing that out further. Thanks a lot!
Zorkus
@Zorkus be sure to test and not just assume I'm right, I'm pretty sure I've tested this in the past and found it accurate, but it's probably very OS/Java Version/phase of the moon dependent.
Bill K
Surefire, all possibly solutions have to be tested..
Zorkus
+1  A: 

I'd suggest checking the Apache Commons IO library. They have some pretty helpful methods for deleting files in the FileUtils class.

ARKBAN
So i did testing of File.delete, and found it removes a file in like 0.5-1 millisecond in avg (tested on cookedup sets of 1000, 10000, 50000 of small files generated).Thanks everybody for ideas! it should work for me..
Zorkus