批量上传相册 将一个文件夹下的所有图片,复制到另一个文件夹 仅给出文件夹目录

package com.hp.jwq;

import java.io.*;

/**
 * 批量上传相册
 *     将一个文件夹下的所有图片,复制到另一个文件夹
 *     仅给出文件夹目录
 */
public class Jwq {
    public static void main(String[] args) {
        File file1 = new File("D:\\焦");
        File file2 = new File("D:\\新建文件夹");
        copy(file1, file2);
    }
    public static void copy(File file1,File file2){
        //获取file1子文件
       File[] imgs = file1.listFiles();
       //遍历,每次循环都是一个要复制的文件
        for (int i = 0; i < imgs.length; i++) {
            //System.out.println(imgs[i]);
            //通过io流把文件边读边写,复制到file2目录中
            try {
                FileInputStream in = new FileInputStream(imgs[i]);
                File imgFile = new File(file2,imgs[i].getName());
                //System.out.println("复制到img2文件夹下的图片的路径="+imgFile);
                FileOutputStream out = new FileOutputStream(imgFile);
                byte[] bytes=new byte[1024];
                int len = 0;
                while ((len=in.read(bytes))!=-1){
                    out.write(bytes,0,len);
                }
                out.close();
                in.close();
                System.out.println("图片["+imgs[i].getName()+"]打印完毕");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值