拷贝文件数据

/**
     * 用途:拷贝文件数据
     * @param sourceFilePath 参数:源文件的路径
     * @param toFilePath 参数:目标的父级路径
     * @throws FileNotFoundException
     * 注意事项:目标的父级路径不存在就会新创建一个新的文件路径
     */
    public static void copyFile(String sourceFilePath, String toFilePath) throws FileNotFoundException {
        // 初始化变量
        FileInputStream in = null;
        FileOutputStream ou = null;
        File file = new File(sourceFilePath);
        // 检测源文件是否存在
        if (!file.exists()) {
            throw new FileNotFoundException(file.getName() + "不存在!....");
        }
        // 目标文件夹是否存在。不存在就新建一个新的文件夹
        File to = new File(toFilePath);
        if (!to.exists()) {
            to.mkdirs();
        }
        // 创建输入输出对象
        in = new FileInputStream(file);
        ou = new FileOutputStream(new File(toFilePath, file.getName()));
        // 创建存储文件的byte数组
        byte[] buffer = new byte[1024 * 8];
        int len = 0;
        // 拷贝文件
        try {
            while ((len = in.read(buffer)) != -1) {
                ou.write(buffer, 0, len);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (ou != null) {
                try {
                    ou.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值