Java 复制 文件夹下的图片格式文件到指定文件夹下


/**
 * @author qiu
 * @date 2023/11/14--13:51
 * 复制某个文件夹下的图片
 **/
public class CopyImg {

    //用于统计复制了多少张图片
    static int imgNums = 0;
    static int jpgNums = 0;
    static int jpegNums = 0;
    static int pngNums = 0;
    static int bmpNums = 0;
    static int gifNums = 0;
    public static void main(String[] args) {
        //源文件夹
        File file = new File("D:\\");
        //存储的目标文件夹
        String endPath = "C:\\Users\\Administrator\\Desktop\\images";
        long begin = System.currentTimeMillis();
        copyFileImg(file, endPath);
        long end = System.currentTimeMillis();
        System.out.println("总共复制了:"+imgNums+"张图片,耗时:"+(end-begin)+"毫秒");
        System.out.println("其中jpg格式的图片有:"+jpgNums);
        System.out.println("其中jpeg格式的图片有:"+jpegNums);
        System.out.println("其中png格式的图片有:"+pngNums);
        System.out.println("其中bmp格式的图片有:"+bmpNums);
        System.out.println("其中gif格式的图片有:"+gifNums);

    }

    private static void copyFileImg(File file,String endPath){
        File[] files = file.listFiles();
        //统计复制了多少照图片
        if (files != null){
            for (File file1 : files) {
                if (file1.isDirectory()){
                    copyFileImg(file1,endPath);
                }else {
                    String name = file1.getName();
                    //判断文件是否是图片格式
                    if (name.endsWith(".jpg")||name.endsWith(".jpeg")||name.endsWith(".png")||name.endsWith(".bmp")||name.endsWith(".gif")){
                        try {
                            FileInputStream fis = new FileInputStream(file1);
                            FileOutputStream fos = new FileOutputStream(endPath + "\\" + name);
                            //设置一次读入多少个字节
                            byte[] bytes = new byte[1024];
                            int len;
                            while ((len = fis.read(bytes)) != -1){
                                fos.write(bytes,0,len);
                            }
                            //释放资源
                            fis.close();
                            fos.close();
                            if (name.endsWith(".jpg")){
                                jpgNums++;
                            } else if (name.endsWith(".jpeg")){
                                jpegNums++;
                            } else if (name.endsWith(".png")){
                                pngNums++;
                            } else if (name.endsWith(".bmp")){
                                bmpNums++;
                            } else if (name.endsWith(".gif")){
                                gifNums++;
                            }else {
                                System.out.println("啥也不是"+name);
                            }
                            imgNums++;
                        } catch (Exception e){
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}

注意:拷贝文件过程有两种写法

方法一:

FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(endPath + "\\" + name);            
byte[] bytes = new byte[1024];
int len;
while ((len = fis.read(bytes)) != -1) {
    fos.write(bytes, 0, len);
}

这段代码表示使用了缓冲区(byte array),每次从输入流fis中最多读取1024个字节到缓冲区,然后将实际读取到的字节数写入到输出流fos中。优点:通过效率较高,减少了读取文件的次数,通过缓冲提高了性能。

方法二:

FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(endPath + "\\" + name);   
int len;
while ((len = fis.read()) != -1) {
    fos.write(len);
}

这端代码每次只从输入流fis中读取一个字节,然后立即将它写入到输出流fos中。这是一个逐个字节复制的方式。相对于上一种使用缓冲区的方法,它会导致更多的文件读写操作,拷贝速度较慢。

总结:在大多数情况下,使用缓冲区方式是更好的选择,它可以减少对文件系统的访问次数,提高性能。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值