Java,IO - 删除文件的最快方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3355746/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 01:27:28  来源:igfitidea点击:

Java, IO - fastest way to remove file

javafileionio

提问by Zorkus

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.

因此,我需要不时删除这些小文件以防止它们占用所有空间。我想将它附加到写入这些文件并将它们加载到数据库中作为最后一步的活动。

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

我的问题是——在 Java 中如何以较少的性能开销删除一堆小文件?

Thanks in advance! Michael

提前致谢!迈克尔

采纳答案by Bozho

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

嗯,file.delete()应该够了(内部实现为原生方法)

回答by ARKBAN

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

我建议检查Apache Commons IO库。他们有一些非常有用的方法来删除 FileUtils 类中的文件。

回答by Bill K

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!

确保在 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.

del 的好处或 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 并删除 a*。

回答by Chris

FileUtils.cleanDirectory(new File("/usr/share/test")); //linux

FileUtils.cleanDirectory(new File("C:\test")); //windows

回答by mcacorner

One can you use java.nio.file.Files's below method

你可以使用java.nio.file.Files下面的方法吗

delete(Path path)
deleteIfExists(Path path)

For more information refer this article

有关更多信息,请参阅这篇文章