Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, IO - fastest way to remove file

Tags:

java

file

io

nio

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

like image 524
Zorkus Avatar asked Jul 28 '10 17:07

Zorkus


People also ask

How do I delete multiple files in Java?

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.

How do I delete multiple files in a directory in Java?

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.

How do you force delete a file in Java?

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.

How do I remove all traces of a file?

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.


2 Answers

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

like image 100
Bozho Avatar answered Sep 22 '22 17:09

Bozho


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

like image 40
ARKBAN Avatar answered Sep 20 '22 17:09

ARKBAN