即空目录可以用File.delete()删除,那么删除非空的目录用什么?

解决方案 »

  1.   

    public static void deleteFileDictionary(File file)throws IOException{
    if(!file.exists()){
    throw new IOException("文件夹"+file.getAbsoluteFile()+"不存在!");
    }
    if(file.exists()&&!file.isDirectory()){
    throw new IOException("文件"+file.getAbsolutePath()+"必须是文件夹!");
    }
    deleteFile(file);
    }
    public static void deleteFile(File file)throws IOException{
    if(!file.isDirectory()){
    if(!file.delete()){
    throw new IOException("..");
    }
    }
    else{
    File[] files=file.listFiles();
    for (int i = 0; i < files.length; i++) {
    deleteFile(files[i]);
    }
    if(file.delete()){
    throw new IOException("..");
    }
    }
    }