本文介绍了从java中删除文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在Java中,我想删除包含文件和文件夹的文件夹中存在的所有内容。
public void startDeleting(String path){
列表< String> filesList = new ArrayList< String>();
列表< String> folderList = new ArrayList< String>();
fetchCompleteList(filesList,folderList,path);
for(String filePath:filesList){
文件tempFile = new File(filePath);
tempFile.delete();
}
for(String filePath:folderList){
文件tempFile = new File(filePath);
tempFile.delete();
}
}
private void fetchCompleteList(List< String> filesList,
List< String> folderList,String path){
文件文件= new文件路径);
文件[] listOfFile = file.listFiles();
for(File tempFile:listOfFile){
if(tempFile.isDirectory()){
folderList.add(tempFile.getAbsolutePath());
fetchCompleteList(filesList,
folderList,tempFile.getAbsolutePath());
} else {
filesList.add(tempFile.getAbsolutePath());
}
}
}
这段代码不起作用,最好的方法是什么?
解决方案
如果你使用它是一个单行:
FileUtils.deleteDirectory(DIR);
请参阅
这几个版本以前从Guava中删除。 >
虽然上面的版本很简单,但它也是非常危险的,因为它没有告诉你很多假设。所以虽然在大多数情况下可能是安全的,但我更喜欢官方方式(Java 7):
public static void deleteFileOrFolder(final Path path)throws IOException {
Files.walkFileTree(path,new SimpleFileVisitor< Path>(){
@Override public FileVisitResult visitFile(final Path file,final BasicFileAttributes attrs)
throws IOException {
Files.delete(file);
return CONTINUE;
}
@Override public FileVisitResult visitFileFailed(final Path file,final IOException e) {
return handleException(e);
}
private FileVisitResult handleException(final IOException e){
e.printStackTrace(); //替换更强大的错误处理
返回TERMINATE;
}
@Override public FileVisitResult postVisitDirectory(final Path dir,final IOException e)
throws IOException {
if(e! = null)return handleException(e);
Files.delete(dir);
返回CONTINUE;
}
});
};
In Java, I want to delete all the contents that are present in a folder which includes files and folders.
public void startDeleting(String path) {
List<String> filesList = new ArrayList<String>();
List<String> folderList = new ArrayList<String>();
fetchCompleteList(filesList, folderList, path);
for(String filePath : filesList) {
File tempFile = new File(filePath);
tempFile.delete();
}
for(String filePath : folderList) {
File tempFile = new File(filePath);
tempFile.delete();
}
}
private void fetchCompleteList(List<String> filesList,
List<String> folderList, String path) {
File file = new File(path);
File[] listOfFile = file.listFiles();
for(File tempFile : listOfFile) {
if(tempFile.isDirectory()) {
folderList.add(tempFile.getAbsolutePath());
fetchCompleteList(filesList,
folderList, tempFile.getAbsolutePath());
} else {
filesList.add(tempFile.getAbsolutePath());
}
}
}
This code does not work, what is the best way to do this?
解决方案
If you use Apache Commons IO it's a one-liner:
FileUtils.deleteDirectory(dir);
See FileUtils.deleteDirectory()
This has been removed from Guava several releases ago.
While the above version is very simple, it is also pretty dangerous, as it makes a lot of assumptions without telling you. So while it may be safe in most cases, I prefer the "official way" to do it (since Java 7):
public static void deleteFileOrFolder(final Path path) throws IOException {
Files.walkFileTree(path, new SimpleFileVisitor<Path>(){
@Override public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs)
throws IOException {
Files.delete(file);
return CONTINUE;
}
@Override public FileVisitResult visitFileFailed(final Path file, final IOException e) {
return handleException(e);
}
private FileVisitResult handleException(final IOException e) {
e.printStackTrace(); // replace with more robust error handling
return TERMINATE;
}
@Override public FileVisitResult postVisitDirectory(final Path dir, final IOException e)
throws IOException {
if(e!=null)return handleException(e);
Files.delete(dir);
return CONTINUE;
}
});
};
这篇关于从java中删除文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!