本文介绍了递归删除导致堆栈溢出错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我问了一个有关如何从目录中的文件夹删除所有文件但保留文件夹的问题,可以在这里找到:
I asked a question about how to delete all files from folders in a directory but keep the folders, this can be found here:
:
Recursion is asking for trouble when there are much simpler solutions. With commons-io:
import java.io.File;
import org.apache.commons.io.FileUtils;
import static org.apache.commons.io.filefilter.TrueFileFilter.TRUE;
File root = new File("D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/");
Iterator<File> files = FileUtils.iterateFiles(root, TRUE, TRUE);
for (File file : files) {
file.delete();
}
或带有:
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
Path root = Paths.get("D:/Documents/NetBeansProjects/printing~subversion/fileupload/web/resources/pdf/");
Files.walkFileTree(root, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
file.delete();
return FileVisitResult.CONTINUE;
}
})
这篇关于递归删除导致堆栈溢出错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!