import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileDeleteStrategy;
/*
* 需要导入commons-io的jar包,下载地址:(选择bin,不是src)
* http://commons.apache.org/proper/commons-io/download_io.cgi;
* 导入jar包的方式很简单,百度查找eclipse怎么导入jar包;
*/
public class FileDeleteStrategyTest
{
public static void main(String[] args)
{
File file = new File("d:\\a\\b"); //创建要删除的文件夹对象;
System.out.println("获得所有文件的绝对路径:");
File[] lists = file.listFiles();
for(File x:lists)
{
System.out.println(x.getAbsolutePath());
}
FileDeleteStrategy strategy = FileDeleteStrategy.NORMAL;
System.out.println("以普通策略删除非空文件夹:");
try
{
strategy.delete(file);
System.out.println("文件删除成功!");
} catch (IOException e)
{
System.out.println("文件删除失败!");
}
strategy = FileDeleteStrategy.FORCE;
System.out.println("以强制策略删除非空文件夹:");
try
{
strategy.delete(file);
System.out.println("文件删除成功!");
} catch (IOException e)
{
System.out.println("文件删除失败!");
}
}
}