0
点赞
收藏
分享

微信扫一扫

Java递归查询文件下所有的图片,移动到指定文件夹中,分批次建立子文件夹


1. 代码实例

/Users/shiheng/desktop/测试文件 目录下的图片(包含子文件夹目录下的图片)移动到了 /Users/shiheng/desktop/测试结果 目录下,默认不分批次建立子文件夹,重名文件只保存一个,代码如下所示:

package com.syrdbt.java.study;

import java.io.File;
import java.util.*;

/**
 * @author syrdbt
 * @date 2020-10-09
 */
public class FileUtils {

    public static void main(String[] args) {

        File inputFile = new File("/Users/shiheng/desktop/测试文件");
        String outputPath = "/Users/shiheng/desktop/测试结果";

        List<File> files = FileUtils.listAllImageFile(inputFile);
        System.out.println("图片文件总数量:" + files.size());

        // 按照名称进行排序
        files.sort(new Comparator<File>() {
            @Override
            public int compare(File file1, File file2) {
                return file1.getName().compareTo(file2.getName());
            }
        });

        // 分批次存储标志, true:分批次存储,false:不分批次存储
        boolean batchSaveFlag = false;
        // 一批次的文件数量
        int batchFileNumber = 10;
        int totalFileSize = files.size();

        // 不分批次
        if (!batchSaveFlag) {
            for (File file : files) {
                String newFilePath = outputPath + "/" + file.getName();
                File newFile = new File(newFilePath);
                file.renameTo(newFile);
            }
            return;
        }

        // 分批次
        for (int i = 0; i < totalFileSize; i += batchFileNumber) {
            int start = i + 1;
            int end = start + batchFileNumber - 1;
            String newDirPath = outputPath + "/" + start + "-" + end;
            File newChildDir = new File(newDirPath);
            //如果文件夹不存在,创建文件夹
            if (!newChildDir.exists()) {
                newChildDir.mkdir();
            }

            for (int j = 0; j < batchFileNumber; j++) {
                if (i + j >= files.size()) {
                    break;
                }
                String newFilePath = newDirPath + "/" + files.get(i + j).getName();
                File newFile = new File(newFilePath);
                files.get(i + j).renameTo(newFile);
            }
        }
    }

    /**
     * 查询文件夹下的所有图片文件
     */
    public static List<File> listAllImageFile(File file) {
        List<File> resultList = new ArrayList<>();
        if (file == null || file.listFiles() == null || 0 == file.listFiles().length) {
            return new ArrayList<File>();
        }

        for (File tempFile : file.listFiles()) {
            if (tempFile.isDirectory()) {
                // 递归遍历文件夹
                resultList.addAll(listAllImageFile(tempFile));
            } else {
                if (isImage(tempFile)) {
                    resultList.add(tempFile);
                }
            }
        }
        return resultList;
    }

    /**
     * 判断一个文件是不是图片文件
     */
    public static boolean isImage(File file) {
        if (file == null) {
            return false;
        }

        String fileName = file.getName();
        if (fileName == null || "".equals(fileName)) {
            return false;
        }

        List<String> splitFileNames = Arrays.asList(fileName.split("\\."));
        if (splitFileNames.size() != 2) {
            System.out.println("错误的文件格式,路径" + file.getPath() + " 名称:" + file.getName());
            return false;
        }

        String fileSuffix = splitFileNames.get(1);
        if (fileSuffix.equals("jpg") || fileSuffix.equals("png") | fileSuffix.equals("jpeg")
                || fileSuffix.equals("JPG") || fileSuffix.equals("PNG") || fileSuffix.equals("JPEG")) {
            return true;
        }
        System.out.println("错误的文件格式,路径" + file.getPath() + " 名称:" + file.getName());
        return false;
    }

}

2. 测试

测试数据, /Users/shiheng/desktop/测试文件 下的子文件夹和图片,如下图所示:

Java递归查询文件下所有的图片,移动到指定文件夹中,分批次建立子文件夹_java


手动创建 /Users/shiheng/desktop/测试结果 文件夹。

Java递归查询文件下所有的图片,移动到指定文件夹中,分批次建立子文件夹_List_02


运行main 函数,结果如下图所示(那些错误的文件格式可以忽略,.DS_Store(英文全称 Desktop Services Store)是一种由苹果公司的macOS操作系统所创造的隐藏文件,目的在于存贮目录的自定义属性):

Java递归查询文件下所有的图片,移动到指定文件夹中,分批次建立子文件夹_java_03


移动到测试结果文件夹的图片如下所示,只有31个,同名文件只留下一个(xugao1.jpg 同名):

Java递归查询文件下所有的图片,移动到指定文件夹中,分批次建立子文件夹_desktop_04

3. 图片分批次存储

将测试结果文件夹的图片分批次保存在该文件夹(测试结果文件)中,文件会按照名称排序再移动到子文件夹中,上面的示例代码修改 inputFile和batchSaveFlag:

package com.syrdbt.java.study;

import java.io.File;
import java.util.*;

/**
 * @author syrdbt
 * @date 2020-10-09
 */
public class FileUtils {

    public static void main(String[] args) {

        File inputFile = new File("/Users/shiheng/desktop/测试结果");
        String outputPath = "/Users/shiheng/desktop/测试结果";

        List<File> files = FileUtils.listAllImageFile(inputFile);
        System.out.println("图片文件总数量:" + files.size());

        // 按照名称进行排序
        files.sort(new Comparator<File>() {
            @Override
            public int compare(File file1, File file2) {
                return file1.getName().compareTo(file2.getName());
            }
        });

        // 分批次存储标志, true:分批次存储,false:不分批次存储
        boolean batchSaveFlag = true;
        // 一批次的文件数量
        int batchFileNumber = 10;
        int totalFileSize = files.size();

        // 不分批次
        if (!batchSaveFlag) {
            for (File file : files) {
                String newFilePath = outputPath + "/" + file.getName();
                File newFile = new File(newFilePath);
                file.renameTo(newFile);
            }
            return;
        }

        // 分批次
        for (int i = 0; i < totalFileSize; i += batchFileNumber) {
            int start = i + 1;
            int end = start + batchFileNumber - 1;
            String newDirPath = outputPath + "/" + start + "-" + end;
            File newChildDir = new File(newDirPath);
            //如果文件夹不存在,创建文件夹
            if (!newChildDir.exists()) {
                newChildDir.mkdir();
            }

            for (int j = 0; j < batchFileNumber; j++) {
                if (i + j >= files.size()) {
                    break;
                }
                String newFilePath = newDirPath + "/" + files.get(i + j).getName();
                File newFile = new File(newFilePath);
                files.get(i + j).renameTo(newFile);
            }
        }
    }

    /**
     * 查询文件夹下的所有图片文件
     */
    public static List<File> listAllImageFile(File file) {
        List<File> resultList = new ArrayList<>();
        if (file == null || file.listFiles() == null || 0 == file.listFiles().length) {
            return new ArrayList<File>();
        }

        for (File tempFile : file.listFiles()) {
            if (tempFile.isDirectory()) {
                // 递归遍历文件夹
                resultList.addAll(listAllImageFile(tempFile));
            } else {
                if (isImage(tempFile)) {
                    resultList.add(tempFile);
                }
            }
        }
        return resultList;
    }

    /**
     * 判断一个文件是不是图片文件
     */
    public static boolean isImage(File file) {
        if (file == null) {
            return false;
        }

        String fileName = file.getName();
        if (fileName == null || "".equals(fileName)) {
            return false;
        }

        List<String> splitFileNames = Arrays.asList(fileName.split("\\."));
        if (splitFileNames.size() != 2) {
            System.out.println("错误的文件格式,路径" + file.getPath() + " 名称:" + file.getName());
            return false;
        }

        String fileSuffix = splitFileNames.get(1);
        if (fileSuffix.equals("jpg") || fileSuffix.equals("png") | fileSuffix.equals("jpeg")
                || fileSuffix.equals("JPG") || fileSuffix.equals("PNG") || fileSuffix.equals("JPEG")) {
            return true;
        }
        System.out.println("错误的文件格式,路径" + file.getPath() + " 名称:" + file.getName());
        return false;
    }

}

分批次保存如下图所示,测试结果文件夹里有4个子文件夹:

Java递归查询文件下所有的图片,移动到指定文件夹中,分批次建立子文件夹_java_05


举报

相关推荐

0 条评论