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
You can use the FileUtils. cleanDirectory() method to recursively delete all files and subdirectories within a directory, without deleting the directory itself. To delete a directory recursively and everything in it, you can use the FileUtils. deleteDirectory() method.
FileUtils; FileUtils. cleanDirectory(directory); There is this method available in the same file. This will also recursively deletes all sub-folders and files under them.
To force delete file using Java, we can use the FileUtils or FileDeleteStrategy class available in Apache Commons Io. We can also use FileDeleteStrategy class of apache commons io to force delete file, even if the file represents a non-enpty directory . the delete() method deletes the file object.
To permanently delete files on Windows, send them to the Recycle Bin and then empty the Recycle Bin to delete them for good. Once the bin is empty, you can't recover the files unless you have data or file recovery software. And even that may not work, because recovery software is no guarantee.
Well, file.delete()
should suffice (it is internally implemented as a native method)
I'd suggest checking the Apache Commons IO library. They have some pretty helpful methods for deleting files in the FileUtils class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With