读取一个目录下的所有excel文件,进行排序 并且判断当前文件夹下面跟目标文件对比有没有缺少

先说思路
1、java程序读取到指定目录下的所有文件
2、将读取到的所有文件进行排序
3、设置一个排序后目标文件顺序
4、先判断目录文件下和目标文件是否匹配
5、在进行目录文件和目标文件进行排序

上代码

@GetMapping("/initData")
private String initData() {
    try {
        // create new file
        File directory = new File("/doc");
        File[] files = directory.listFiles();
        if (files != null) {
            //操作文件导入数据 排序好的
            List<File> files1 = fileSort(files);
        }
    } catch (Exception e) {
        // if any error occurs
        e.printStackTrace();
    }
    return "";
}


public List<File> fileSort(File[] files) {
    List<File> files1 = new ArrayList<>();
    for (File file : files) {
        if (file.isFile()) {
            String name = file.getName();
            if (name.lastIndexOf('.') > 0) {
                files1.add(file);
            }
        }
    }
    //目标文件顺序
    String[] desiredOrder = {
            "公司资料",
            "公司组织架构资料",
            "公司人员用户资料",
            "客户资料",
            "供应商资料",
            "产品类别资料",
            "产品信息资料",
            "仓库资料",
            "工序资料",
            "工艺资料",
            "机台资料",
            "岗位安排资料"
    };

    // 检查desiredOrder中哪些文件不存在于files1中,并打印提示
    List<String> missingFiles = new ArrayList<>();
    for (String desiredFile : desiredOrder) {
        String fileName = desiredFile + ".xls";
        if (!containsFileName(files1, fileName)) {
            missingFiles.add(fileName);
        }
    }
    if (!missingFiles.isEmpty()) {
        System.out.println("缺少以下文件:");
        for (String missingFile : missingFiles) {
            System.out.println(missingFile);
        }
    }

    // 根据目标顺序对原始列表进行排序
    Collections.sort(files1, new Comparator<File>() {
        @Override
        public int compare(File file1, File file2) {
            String name1 = file1.getName().replace(".xls", "");
            String name2 = file2.getName().replace(".xls", "");
            int index1 = indexOf(desiredOrder, name1);
            int index2 = indexOf(desiredOrder, name2);
            if (index1 == -1 && index2 == -1) {
                return name1.compareTo(name2); // 如果两个元素都不在目标顺序中,按照自然顺序排序
            } else if (index1 == -1) {
                return 1; // 如果o1不在目标顺序中,放在排序后列表的末尾
            } else if (index2 == -1) {
                return -1; // 如果o2不在目标顺序中,放在排序后列表的末尾
            }
            return Integer.compare(index1, index2);
        }
    });

    System.out.println("--------------排序--------------");
    // 打印排序后的列表
    for (File file : files1) {
        System.out.println(file.getName());
    }
    return files1;
}


private static boolean containsFileName(List<File> files, String fileName) {
    for (File file : files) {
        if (file.getName().equals(fileName)) {
            return true;
        }
    }
    return false;
}

private static int indexOf(String[] array, String value) {
    for (int i = 0; i < array.length; i++) {
        if (array[i].equals(value)) {
            return i;
        }
    }
    return -1; // 如果找不到,返回-1
}
posted @ 2024-11-25 17:23  泡泡大可爱  阅读(45)  评论(0)    收藏  举报