将一个文件夹下的所有图片,复制到另一个文件夹

public class Zuoye1 {
    public static void main(String[] args) throws Exception {
        File file1 = new File("C:\\Users\\Lenovo\\Desktop\\壁纸");
        String q="C:\\Users\\Lenovo\\Desktop\\bizhi\\";
        File file2 = new File(q);
        //判断是否有改路径,没有创建
        if (!file2.exists()){
            //创建新文件
            file2.mkdir();
        }
        File[] files = file1.listFiles();
        for (File file:files) {
            InputStream in = new FileInputStream(file);
            String src=q+file.getName();
            OutputStream out = new FileOutputStream(src);
            byte[] bytes = new byte[1024];
            int a=-1;
            while((a=in.read(bytes))!=-1){
                out.write(bytes,0,a);
            }
            in.close();
            out.close();
        }



    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Python中的shutil模块来实现将一个文件夹下的图片复制到另一个文件夹下。下面是一个简单的示例代码: ```python import os import shutil def copy_images(source_folder, destination_folder): # 检查目标文件夹是否存在,如果不存在则创建 if not os.path.exists(destination_folder): os.makedirs(destination_folder) # 遍历源文件夹中的所有文件 for filename in os.listdir(source_folder): # 获取文件的完整路径 source_file = os.path.join(source_folder, filename) # 判断文件是否为图片文件 if os.path.isfile(source_file) and filename.lower().endswith(('.jpg', '.jpeg', '.png', '.gif')): # 构建目标文件的完整路径 destination_file = os.path.join(destination_folder, filename) # 复制图片文件到目标文件夹 shutil.copy2(source_file, destination_file) # 调用函数进行图片复制 copy_images("source_folder_path", "destination_folder_path") ``` 在上面的代码中,你需要替换`source_folder_path`和`destination_folder_path`为实际的源文件夹和目标文件夹的路径。该代码会遍历源文件夹中的所有文件,判断是否为图片文件(以.jpg、.jpeg、.png、.gif为后缀),如果是,则将其复制到目标文件夹中。 注意,使用`shutil.copy2`函数可以保留原始图片的元数据(如创建时间、修改时间等),如果不需要保留元数据,你也可以使用`shutil.copy`函数。此外,该代码假设目标文件夹不存在,如果目标文件夹已存在,可以根据实际需求进行处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值