复制文件/文件夹,删除文件/文件夹

7 篇文章 0 订阅
7 篇文章 0 订阅

复制文件夹:

public static boolean copyFolder(String srcFolderFullPath, String destFolderFullPath) {
        try {
            (new File(destFolderFullPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
            File file = new File(srcFolderFullPath);
            String[] files = file.list();
            File temp = null;
            for (int i = 0; i < files.length; i++) {
                if (srcFolderFullPath.endsWith(File.separator)) {
                    temp = new File(srcFolderFullPath + files[i]);
                } else {
                    temp = new File(srcFolderFullPath + File.separator + files[i]);
                }
                if (temp.isFile()) {
                    FileInputStream input = new FileInputStream(temp);
                    copyFile(input, destFolderFullPath + "/" + (temp.getName()).toString());
                }
                if (temp.isDirectory()) {// 如果是子文件夹
                    copyFolder(srcFolderFullPath + "/" + files[i], destFolderFullPath + "/" + files[i]);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

 复制文件:

public static boolean copyFile(InputStream ins, String destFileFullPath) {
        FileOutputStream fos = null;
        try {
            File file = new File(destFileFullPath);
            fos = new FileOutputStream(file);
            byte[] buffer = new byte[8192];
            int count = 0;
            while ((count = ins.read(buffer)) > 0) {
                fos.write(buffer, 0, count);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                fos.close();
                ins.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

 删除文件:

public static void deleteFile(String targetFileFullPath) {
        File file = new File(targetFileFullPath);
        file.delete();
    }

 删除文件夹:

public static void deleteFolder(String targetFolderFullPath) {
        File file = new File(targetFolderFullPath);
        if (!file.exists()) {
            return;
        }
        String[] files = file.list();
        File temp = null;
        for (int i = 0; i < files.length; i++) {
            if (targetFolderFullPath.endsWith(File.separator)) {
                temp = new File(targetFolderFullPath + files[i]);
            } else {
                temp = new File(targetFolderFullPath + File.separator + files[i]);
            }
            if (temp.isFile()) {
                deleteFile(targetFolderFullPath + "/" + (temp.getName()).toString());
            }
            if (temp.isDirectory()) {// 如果是子文件夹
                deleteFolder(targetFolderFullPath + "/" + files[i]);
            }
        }
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值