Java File.delete()不会删除所有文件 [英] Java File.delete() does not delete all files

查看:164
本文介绍了Java File.delete()不会删除所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Java代码,它会遍历目录中的所有文件并将其删除。

I have the following Java code which iterates through all the files in a directory and deletes them.

for(File file : tmpDir.listFiles())
{
    file.delete();
}

但是它不会删除所有文件。当我这样做时,通常会留下几千个中的20-30个。是否有可能解决此问题,或者我偶然发现了一些最好单独使用的Java伏都教徒?

It does however not delete all files. Some, usually 20-30, out of a couple of thousand, are left behind when I do this. Is it possible to fix this, or have I stumbled upon some Java voodoo that is best left alone?

推荐答案

强制垃圾回收器可以使用 System.gc(); 来删除所有文件。

Forcing the garbage collector to run using System.gc(); made all the files deletable.

这篇关于Java File.delete()不会删除所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆