java - 如何创建具有平均深度的文件夹和子文件夹

标签 java directory subdirectory

我有一个类,它创建根文件夹“foo”及其子文件夹:

public class FolderCreator {

    public static boolean createDirectoriesWithCommonParent(File parent, String...subs) {

        parent.mkdirs();
        if (!parent.exists() || !parent.isDirectory()) {
            return false;
        }

        for (String sub : subs) {
            File subFile = new File(parent, sub);
            subFile.mkdir();
            if (!subFile.exists() || !subFile.isDirectory()) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        createDirectoriesWithCommonParent(new File("test/foo"), "a", "b", "c");
    }
}

我设置了数字 SUBFOLDERS_COUNT,并且希望创建平均深度 = SUBFOLDERS_COUNT/3 的文件夹和子文件夹。

如何创建具有给定平均深度的文件夹和子文件夹?

最佳答案

我发现我的问题了! 为了解决这个任务,以下算法是合适的:

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class FolderCreator {
    private static final int SUBFOLDERS_COUNT = 9;
    private static final String BASE_PATH = "test/foo";

    public static void main(String[] args) {
        List<String> subFolders = createSubFolders(SUBFOLDERS_COUNT, SUBFOLDERS_COUNT/3);
        for (String subFolder : subFolders) {
            System.out.println(subFolder);
        }
    }

    private static List<String> createSubFolders(int subFoldersCount, int firstLevelFoldersCount) {
        List<String> paths = new ArrayList<>();
        while (firstLevelFoldersCount != 0) {
            String folderName = BASE_PATH;
            Random random = new Random();
            int max = subFoldersCount - firstLevelFoldersCount + 1;
            int depth = firstLevelFoldersCount == 1 ? subFoldersCount : 1 + random.nextInt(max);
            firstLevelFoldersCount--;
            subFoldersCount -= depth;
            for (int i = 0; i < depth; i++) {
                int layerNumber = i + 1;
                int sequenceNumber = 0;
                folderName = folderName + "\\" + layerNumber + "st" + "LayerSubFolder" + sequenceNumber;
                while (paths.contains(folderName)) {
                    sequenceNumber++;
                    folderName = folderName.substring(0, folderName.length() - 1) + sequenceNumber;
                }
                paths.add(folderName);
                File file = new File(folderName);
                file.mkdirs();
            }
        }
        return paths;
    }
}

文件夹结构如图所示:

The folder structure will as shown in the picture for example

关于java - 如何创建具有平均深度的文件夹和子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62000550/

相关文章:

apache 别名和子目录

powershell - 使用更好的 PowerShell 脚本将文件应用到多个文件夹

java - 充气失败

makefile - Makefile : How to place auto-generated files in a separate directory?

windows - Bitvise SSH + Git + 初始目录

oracle - 如何在oracle home中创建目录

java - 扫描对象的空属性

java - 在 Spring MVC 框架中不使用 MessageSourceAware 访问 MessageSource 的方法

Java JSON 返回一个数组作为值

php - 我有两个 php 文件,其中一个文件位于子文件夹内,我想转到 php 文件之外