java io 操作中没有直接删除文件目录的方法,一般需要自己写一些辅助工具类,现贴自己写的一个目录的例子
import java.io.File;
public class DirectDelete {
public static boolean deleteFile(File file )
{
System.out.println( file.getPath() );
boolean returnFlag = true;
if (file.isFile())
{
returnFlag = file.delete();
}
else
{
returnFlag = deleteDir(file);
}
return returnFlag;
}
private static boolean deleteDir(File file)
{
boolean returnFlag= true;
if (file.isDirectory())
{
File[] fs = file.listFiles();
for(File f:fs)
{
returnFlag = deleteFile( f );
if( returnFlag ==false )
break;
}
returnFlag = file.delete();
}
return returnFlag;
}
/**
* @param args
*/
public static void main(String[] args) {
String path = "D:/apache-archiva";
File file = new File(path);
long beginTime = System.currentTimeMillis();
DirectDelete.deleteFile(file);
long endTime = System.currentTimeMillis();
System.out.println(" use times :"+(endTime-beginTime));
}
}