java实现图片拷贝(将超大文件夹拆分成小文件夹存放图片)

版权声明:本文为博主原创文章,转载必须注明出处。https://blog.csdn.net/beidouxin2010wang/article/details/83190328

import java.io.*;

public class PicDeal {
    //原始图片文件夹
     static String originalPath = "D:\\1";
    //复制文件地址
     static String copyPath = "D:\\2";
    //复制图片的方法
    private static void copyPhoto(File file) throws IOException {
        //将路径封装成File数组
        File[] files = file.listFiles();
        int j = 0;
        //遍历这个数组
        for (int i = 0; i<files.length; i++) {
            //判断是否是文件夹
            if (files[i].isDirectory()) {
                //是的话就继续调用这个方法
                copyPhoto(files[i]);  
            } else { //不是的话就匹配是否是图片
                //每100张照片生成一个文件夹
                if(i%100 == 0) {
                    j++;
                    //生成文件夹
                    File file2 = new File(copyPath+"\\"+j);
                    file2.mkdir();
                }  //创建输入文件字节流
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(files[i]));
                    byte[] buf = new byte[1024 * 20];
                    int lenght = 0;
                    //创建输出文件字节流包含文件输出位置
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream( 
                            copyPath +"\\"+j+"\\"+ files[i].getName())); 
                    while ((lenght = bis.read(buf)) != -1) {
                        bos.write(buf, 0, lenght);
                        //这一步很重要,没有它图片容易损毁
                        bos.flush();
                        if(i == files.length-1) {
                            bos.close();
                            bis.close();
                            System.out.println("#############复制完成!!!#################");
                            return;
                        }
                }
            }
        }
    }

    public static void main(String[] args) {
        //需要查找的文件夹
        File file = new File(originalPath);
        try {
            copyPhoto(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值