简体   繁体   中英

Delete files / folders with Java 7

I am getting killed on performance with file / folder deletes in Java.

The code is quite old and I am wondering if Java 7 (which I upgraded to) actually offers performance improvements, or just another syntax. (I don't want to retool everything unless there is a benefit). I regularly need to extract large ZIPs and then delete the contents and the recursion time is brutal.

I am also stuck on Windows.

Thanks

I would to suggest to use some kind of jar already provided by community. For example, common-io.xx.jar, spring-core.jar

    Eg, org.apache.commons.io.FileUtils;
        FileUtils.copyDirectory(from, to);
        FileUtils.deleteDirectory(childDir);
        FileUtils.forceDelete(springConfigDir);
        FileUtils.writeByteArrayToFile(file, data);

        org.springframework.util.FileSystemUtils;
        FileSystemUtils.copyRecursively(from, to);
        FileSystemUtils.deleteRecursively(dir);

File IO is very dependant on the performance of your hardware. Many HDD can perform 80 - 120 IOPS per second. If you want to open a file you can read up to 120 files per second. To delete a file it can require two updates or up to 60 files deleted per second. With these constraints there is almost nothing you can do in software which will make any difference.

If you have an SSD however, these can do 80,000 to 230,000 IOPS per second (more than a thousand fold increase) At this point what you do software might make a difference, but as you are dealing with compressed files, it most like that CPU will be your bottleneck at this point.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM