用java语言 模拟回收站的功能

windows操作系统的回收站具有如下功能:设置回收站大小(大小可以随放入回收站的内容而增加)、清空回收站、还原所有项目 、还原选定项目(使用可变参数)、将删除的内容收入回收站等功能。同时无论多少次打开回收站,桌面上只有一个回收站窗口。请使用Java语言编程序,模拟回收站的功能

public class Recycle {
public static Recycle recycle = new Recycle();

public static File file = new File("D:\\Recycle");


public static Long fileSize = 0L;

public static Long fileLimitSize = 1024L;

public static Map<String, String> filePathMap = new HashMap<>();

public static Long getFileLimitSize() {
    return fileLimitSize;
}

public static void setFileLimitSize(Long fileLimitSize) {
    Recycle.fileLimitSize = fileLimitSize;
}

//初始化文件夹
public static void initRecycle() {
    if (!file.exists()) {//如果文件夹不存在
        file.mkdir();//创建文件夹
    }

}

//将删除的内容收入回收站等功能
public static void move2Recycle(File removeFile) {
    countFileSize(removeFile); //每次删除文件计算回收站大小
    UUID uuid = UUID.randomUUID();
    filePathMap.put(uuid.toString(), removeFile.getAbsolutePath());
    //复制整个文件夹
    if (removeFile.isFile()) {
        copy(removeFile.getAbsolutePath(), file.getAbsolutePath() + File.separator + uuid);
    } else {
        copyAllFolder(removeFile.getAbsolutePath(), file.getAbsolutePath() + File.separator + uuid);
    }
    while (fileLimitSize <= fileSize) {
        //扩容计算
        fileLimitSize = fileLimitSize << 2;
    }
    deleteFile(removeFile);
}
//还原所有项目
public static  void reductionAll(){
    File[] files = file.listFiles();
    if (files!=null && files.length>0){
        for (File file1 : files) {
            if (file1.isFile()) {
                copy(file1.getAbsolutePath(), filePathMap.get(file1.getName()));
            } else {
                copyAllFolder(file1.getAbsolutePath(), filePathMap.get(file1.getName()));
            }
            deleteFile(file1);
        }
    }
    filePathMap.clear();
}

//还原选定项目(使用可变参数)
public static  void reductionPart(File...files ){
    for (File file1 : files) {
        if (file1.isFile()) {
            copy(file1.getAbsolutePath(), filePathMap.get(file1.getName()));
        } else {
            copyAllFolder(file1.getAbsolutePath(), filePathMap.get(file1.getName()));
        }
        deleteFile(file1);
        filePathMap.remove(file1.getName());
    }

}


//清空回收站
public static void clean() {
    File[] files = file.listFiles();
    for (File file1 : files) {
        deleteFile(file1);
    }
}


public static boolean deleteFile(File dirFile) {
    // 如果dir对应的文件不存在,则退出
    if (!dirFile.exists()) {
        return false;
    }

    if (dirFile.isFile()) {
        return dirFile.delete();
    } else {
        for (File file2 : dirFile.listFiles()) {
            deleteFile(file2);
        }
    }
    return dirFile.delete();
}


//每次移动至回收站计算回收站大小
public static void countFileSize(File moveFile) {
    if (moveFile.isFile()) {
        fileSize = fileSize + moveFile.length();
    }
    final File[] children = moveFile.listFiles();
    if (children != null) {
        for (final File child : children) {
            countFileSize(child);
        }
    }
}

public static void copyAllFolder(String oldPath, String newPath) {

    try {
        (new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹
        File a = new File(oldPath);
        String[] file = a.list();
        File temp = null;
        for (int i = 0; i < file.length; i++) {
            if (oldPath.endsWith(File.separator)) {
                temp = new File(oldPath + file[i]);
            } else {
                temp = new File(oldPath + File.separator + file[i]);
            }

            if (temp.isFile()) {
                FileInputStream input = new FileInputStream(temp);
                FileOutputStream output = new FileOutputStream(newPath + "/" +
                        (temp.getName()).toString());
                byte[] b = new byte[1024 * 5];
                int len;
                while ((len = input.read(b)) != -1) {
                    output.write(b, 0, len);
                }
                output.flush();
                output.close();
                input.close();
            }
            if (temp.isDirectory()) {//如果是子文件夹
                copyAllFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
            }
        }
    } catch (Exception e) {
        System.out.println("复制整个文件夹内容操作出错");
        e.printStackTrace();

    }

}

static void copy(String srcPathStr, String desPathStr) {
    try {
        FileInputStream fis = new FileInputStream(srcPathStr);//创建输入流对象
        FileOutputStream fos = new FileOutputStream(desPathStr); //创建输出流对象
        byte datas[] = new byte[1024 * 8];//创建搬运工具
        int len = 0;//创建长度
        while ((len = fis.read(datas)) != -1)//循环读取数据
        {
            fos.write(datas, 0, len);
        }
        fis.close();//释放资源
        fis.close();//释放资源
    } catch (Exception e) {
        e.printStackTrace();
    }
}


private Recycle() {

}

public static Recycle getRecycle() {
    return recycle;
}

}

+1同被zlz摧残的人

没想到是校友,牛啊老哥