JAVA 实现文件剪切再删除原有文件夹

java剪切文件

今天改bug遇到个小问题,将文件解压后不想要改文件外面包着的文件夹,要是windows下面直接就是一顿ctrl+x再来个delete。。。

    //如果有一层文件夹,将里面的文件剪切到外面
    File dirFile = new File(unZipDir + dir);
    if (dirFile.isDirectory()) {
        try {
            copyDir(unZipDir.getPath(), dirFile.getPath(), unZipDir.getPath());
        } catch (IOException e) {
            e.printStackTrace();
        }
//            dirFile.delete();
            deleteDir(dirFile, false);
        }

这时候文件已经解压好了,先得到该解压好的文件夹,我这路径按照原有的程序拼接了一哈:unZipDir+dir
如果说解压出来是一个文件夹包着的,就先开始剪切里面的所有文件出来。

public static void copyDir(String path, String sourcePath, String newPath) throws IOException {
    File file = new File(sourcePath);
    String[] files = file.list();

    if (!(new File(newPath)).exists()) {
        (new File(newPath)).mkdir();
    }

    for (int i = 0; i < files.length; i++) {
        if ((new File(sourcePath + file.separator + files[i])).isDirectory()) {
            copyDir(path, sourcePath + file.separator + files[i], path + file.separator + files[i]);
        }

        if (new File(sourcePath + file.separator + files[i]).isFile()) {
            copyFile(sourcePath + file.separator + files[i], newPath + file.separator + files[i]);
        }

    }
}

path:是解压的目录
sourcePath:就是需要剪切出来的文件夹
newPath:就是剪切出来放的路径
比如我在这里插入图片描述把解压的文件放入userPage/1/65下面,解压包为lgg.zip,那么我的 path 就是 userPage/1/65sourcePathuserPage/1/65/lggnewPathuserPage/1/65

看代码,得到sourcePath这个文件以及里面的所有文件,然后创建newPath这个文件夹

tip:因为我们只需要创建一个新的文件夹,所以使用mkdir()这个方法,mkdirs()这个方法会根据你的路径创建多个目录,这里不需要。

然后遍历sourcePath文件夹里面所有文件,根据文件的类型分别创建文件或文件夹,创建文件夹继续调用copyDir()方法,创建文件使用

public static void copyFile(String oldPath, String newPath) throws IOException {
    File oldFile = new File(oldPath);
    File file = new File(newPath);
    FileInputStream in = new FileInputStream(oldFile);
    FileOutputStream out = new FileOutputStream(file);

    byte[] buffer = new byte[2097152];
    int readByte = 0;
    while ((readByte = in.read(buffer)) != -1) {
        out.write(buffer, 0, readByte);
    }

    in.close();
    out.close();
}

注意传参传入路径的区别
file.separator 也就是FIle类固定的一个符号"\"路径分隔符应该叫。
file[i]也就是遍历到的文件名称。
注意复制文件时,如果该文件不是在根目录下,那么他的newPath应该是 新的文件夹路径+file.separator+file[i],我们需要把他放进它本该在的文件夹,不然所有的文件都会暴露在根目录下。。。

所有文件复制完成,最后我们将解压出来的文件夹以及所有里面所有文件删除。
不可直接调用delete()方法,这个dirFile是一个文件夹,如果里面存在任何文件将删除失败,所以还需要遍历去删除其中的数据。

private static boolean deleteDir(File dir, boolean keepRoot) {
    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]), false);
            if (!success) {
                return false;
            }
        }
    }

    // The directory is now empty so now it can be smoked
    if (!keepRoot) {
        return dir.delete();
    } else {
        return true;
    }
}

遍历文件夹的子子孙孙,将所有文件删除,我这边操作是整个文件夹删除,就不保留空文件夹了,keepRoot传false值。

打完收工!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值