java删除文件夹

来源:互联网 发布:软件实施工程师简历 编辑:程序博客网 时间:2024/05/15 12:07

java删除文件夹,先将 文件路径和文件名移动到短路径,再删。


import java.io.File;



/*
 * FileName: DeleteFile.java
 * Author:   qingfeng2556
 * Date:     2016年10月24日 上午9:56:41  
 */


import java.io.File;
import java.nio.file.Files;


/**
 * @date 2016年10月24日 上午10:00:11
 */
public class DeleteLongFolder {
    public static int length;
    public static String path = "\\temp";// 取出来的文件夹的存放位置
    public static File tempFile ;


    public static void main(String[] args) {
        delete("C:\\Users\\Administrator\\Desktop\\工作\\临时\\203web\\usr\\local\\p5000");
    }


    public static void delete(String waitToDelPath) {
        if (waitToDelPath!=null&& waitToDelPath.contains(":")) {
            System.out.println(waitToDelPath.substring(0, 1));
            DeleteLongFolder.path=waitToDelPath.substring(0, 1)+":"+path;
            
        }
        DeleteLongFolder.tempFile=new File(path);
        if (!tempFile.exists()) {
            tempFile.mkdirs();
        }
        long start=System.currentTimeMillis();
        File file = new File(waitToDelPath);/* 路径过长的文件夹 */
        delDir(file, tempFile);
        tempFile.delete();
        System.out.println("cost:"+(System.currentTimeMillis()-start)+"ms");
    }


    public static void delDir(File file, File tempFile) {
        if (file == null || tempFile == null) {
            return;
        }
        File tofile = new File(path, "" + length);  
        length++;
        
        if (file.isFile()) { 
            if (!file.renameTo(tofile)) {
                System.out.println("mv fail:"+file.getAbsolutePath()+" to "+tofile.getAbsolutePath());
            }else{
                System.out.println("mv " + file.getAbsolutePath() + " to " + tofile.getAbsolutePath());
            }
            delEmptyDirOrFile(tofile);
            return;
        } else {
            File[] listFile = file.listFiles();
            if (listFile == null) {
                if (!file.renameTo(tofile)) {
                    System.out.println("mv fail:"+file.getAbsolutePath()+" to "+tofile.getAbsolutePath());
                }else{
                    System.out.println("mv " + file.getAbsolutePath() + " to " + tofile.getAbsolutePath());
                }
                delEmptyDirOrFile(tofile);
                return;
            } else {
                for (File file2 : listFile) {
                    delDir(file2, tempFile);
                }
                if (!file.renameTo(tofile)) {
                    System.out.println("mv fail:"+file.getAbsolutePath()+" to "+tofile.getAbsolutePath());
                }else{
                    System.out.println("mv " + file.getAbsolutePath() + " to " + tofile.getAbsolutePath());
                }
                delEmptyDirOrFile(tofile);
            }
        }
    }
    
    public static boolean  delEmptyDirOrFile(File waitToDelFile){
        if(!waitToDelFile.delete()){
            System.out.println("del fail:" + waitToDelFile.getPath());
            return false;
        }
        return true;
    }
}
0 0
原创粉丝点击