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.
我的問題是我有一個應用程序正在編寫大量相對(100-500kb)的小型CSV文件(數十萬和數十萬)。然后通過sql loader調用(它的oracle db)將這些文件的內容加載到數據庫中,這就是我必須要使用的內容。
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.
因此,我需要不時刪除這些小文件,以防止它們占用所有空間。我想將其附加到寫入這些文件的活動,並將它們作為最后的最終步驟加載到db中。
My Question is -- how in java can one remove a bunch of small files with less overhead on performance?
我的問題是 - 如何在java中刪除一堆性能開銷較小的小文件?
Thanks in advance! Michael
提前致謝!邁克爾
4 个解决方案
#1
13
Well, file.delete() should suffice (it is internally implemented as a native method)
好吧,file.delete()應該足夠了(它在內部實現為本機方法)
#2
4
I'd suggest checking the Apache Commons IO library. They have some pretty helpful methods for deleting files in the FileUtils class.
我建議檢查Apache Commons IO庫。他們有一些非常有用的方法來刪除FileUtils類中的文件。
#3
2
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.
如果你彈出並讓系統刪除它們,你可能會發現它快一個數量級。你必須能夠到達一個停止點(沒有文件正在處理)然后shell並刪除“*”或。或者無論你的操作系統是什么。
(Note, this makes your program VERY os dependent!)
(注意,這會使你的程序非常依賴!)
Be sure on Windows and Mac that you are bypassing the trashcan feature!
在Windows和Mac上確保您繞過了垃圾桶功能!
The nice thing about del . or rm * is that they SHOULD batch the operation rather than repeatedly opening, modifying and closing the directory.
德爾的好消息。或者rm *是他們應該批量操作而不是重復打開,修改和關閉目錄。
You might also write filenames with a pattern like a001, a002, a003, ... and when you reach a999 you go to b001 and delete a*.
您也可以使用a001,a002,a003等模式編寫文件名,當達到a999時,轉到b001並刪除*。
#4
1
FileUtils.cleanDirectory(new File("/usr/share/test")); //linux
FileUtils.cleanDirectory(new File("C:\\test")); //windows