用递归实现的删除文件夹,很简单,功能也很强,非空文件夹也能删除!
package
file;
import java.io.File;
public class DeleteAllFile ... {
public static void main(String args[]) ...{
clearTmpFile(new File("c:/forLiu"));
}
private static void clearTmpFile(File tmpFile) ...{
if (!tmpFile.exists()) ...{
System.out.println("file is not exist!");
return;
}
if (tmpFile.isDirectory()) ...{
File[] fileList = tmpFile.listFiles();
for (int i = 0; i < fileList.length; i++) ...{
if (fileList[i].isDirectory()) ...{
clearTmpFile(fileList[i]);
}
if (fileList[i].delete()) ...{
System.out.println("Temp file:""
+ fileList[i].getAbsolutePath()
+ "" has been deleted.");
}
}
if (tmpFile.delete()) ...{
System.out.println("Temp file:"" + tmpFile.getAbsolutePath()
+ "" has been deleted.");
}
} else ...{
if (tmpFile.delete()) ...{
System.out.println("Temp file:"" + tmpFile.getAbsolutePath()
+ "" has been deleted.");
}
}
}
}
import java.io.File;
public class DeleteAllFile ... {
public static void main(String args[]) ...{
clearTmpFile(new File("c:/forLiu"));
}
private static void clearTmpFile(File tmpFile) ...{
if (!tmpFile.exists()) ...{
System.out.println("file is not exist!");
return;
}
if (tmpFile.isDirectory()) ...{
File[] fileList = tmpFile.listFiles();
for (int i = 0; i < fileList.length; i++) ...{
if (fileList[i].isDirectory()) ...{
clearTmpFile(fileList[i]);
}
if (fileList[i].delete()) ...{
System.out.println("Temp file:""
+ fileList[i].getAbsolutePath()
+ "" has been deleted.");
}
}
if (tmpFile.delete()) ...{
System.out.println("Temp file:"" + tmpFile.getAbsolutePath()
+ "" has been deleted.");
}
} else ...{
if (tmpFile.delete()) ...{
System.out.println("Temp file:"" + tmpFile.getAbsolutePath()
+ "" has been deleted.");
}
}
}
}