将500张图片按一定数量存放到不同的文件夹里,每个文件夹存放20张

本文介绍了一个使用Java编写的简单程序,该程序可以将大量图片按照指定的数量自动分配到不同的文件夹中。例如,可以将500张图片每20张一组分别存放到各个文件夹,便于管理和查看。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将500张图片按一定数量存放到不同的文件夹里
  • 500张图片,每个文件夹存放20张
  • 在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
import java.io.*;

/**
 * @date 2021/02/23
 */
public class CopyFile{
    public static void main(String[] args) {
        File file = new File("D:\\BaiduNetdiskDownload\\tx\\壁纸精选500张(三)");
        // 获得图片名列表
        String[] names = file.list();
        // 获得图片列表
        File[] files = file.listFiles();
        assert names != null;
        assert files != null;
        // 需要创建的文件夹数量,每个文件夹放20张图片
        int dirCount = names.length % 20 == 0 ? names.length / 20 : names.length / 20 + 1;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        for(int i = 0;i < dirCount;i++) {
            File tempFile = new File("D:\\BaiduNetdiskDownload\\tx\\壁纸分组\\壁纸分组" + (i + 1));
            if (!tempFile.exists()) {
                tempFile.mkdirs();
            }
            FileInputStream fis = null;
            FileOutputStream fos = null;
            System.out.println("正在操作壁纸分组" + (i + 1));
            for(int j = i * 20 ; j < i * 20 + 20 && j < files.length;j++){
                System.out.println("正在操作:" + names[j]);
                try {
                    fis = new FileInputStream(files[j]);
                    fos = new FileOutputStream("D:\\BaiduNetdiskDownload\\tx\\壁纸分组\\壁纸分组" + (i + 1)
                    +"\\" + names[j]);
                    bis = new BufferedInputStream(fis);
                    bos = new BufferedOutputStream(fos);
                    byte[] buffer = new byte[1024];
                    int len;
                    while((len = bis.read(buffer)) != -1){
                        bos.write(buffer,0,len);
                        bos.flush();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if(bis != null){
                        try {
                            // 外层流关闭内层流自动关闭,所有不需要fis.close();
                            bis.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if(bos != null){
                        try {
                            // 外层流关闭内层流自动关闭,所有不需要fos.close();
                            bos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            System.out.println("操作壁纸分组" + (i + 1) + "成功");
        }

    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值