【Java文件管理】批量生成文件,随机生成,指定文件夹深度

1. 在指定文件夹底下随机生成文件夹及文件

1.1 参数

参数名含义默认值
baseDir文件根路径/home/yhc/workSpace/log
minSum最小文件数量10
randomSum随机文件数量,最大为minSum+randomSum10
folderMaxDepth生成文件夹深度5
maxFolderCount每层最大文件夹数量10
prefix文件名前缀log_
suffix文件名后缀_log
fileType文件类型_log
fileName文件名file
fileNameRandom文件名随机数长度(含数字、字母)6
charSize设置数组大小为1MB,可以根据需要调整10 * 1024 * 1024
fileSize设置文件大小为1MB,可以根据需要调整10 * 1024 * 1024
fillChar填充字符,用于填充文件大小x

1.2 代码

package com.api.apidemo.tool.file;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Random;

/**
 * @author Y
 * @description 批量生成文件 - 目录深度
 */
public class BatchCreateFileDepth {
    /**
     * 文件根路径
     */
    public static final String baseDir = "/home/cathay10/workSpace/log";
    /**
     * 随机数生成器
     */
    static Random random = new Random();
    /**
     * 最小文件数量
     */
    static int minSum = 5;
    /**
     * 随机文件数量,最大为minSum+randomSum
     */
    static int randomSum = 5;

    /**
     * 生成文件夹深度
     */
    static int folderMaxDepth = 6;

    /**
     * 每层最大文件夹数量
     */
    static int maxFolderCount = 10;
    /**
     * 文件名前缀
     */
    static String prefix = "log_";
    /**
     * 文件名后缀
     */
    static String suffix = "_log";
    /**
     * 文件名后缀
     */
    static String fileType = "txt";
    /**
     * 文件名
     */
    static String fileName = "file";
    /**
     * 文件名随机数长度(含数字、字母)
     */
    static int fileNameRandom = 6;
    /**
     * 设置数组大小为1MB,可以根据需要调整
     */
    static int charSize = 1 * 1024;
    /**
     * 设置文件大小为1MB,可以根据需要调整
     */
    static int fileSize = 1 * 1024;
    /**
     * 填充字符,用于填充文件大小
     */
    static char fillChar = 'x';

    public static void main(String[] args) {
        batchCreateFile();
    }

    /**
     * 创建文件
     */
    private static void batchCreateFile() {
        String outputPath = baseDir;
        Path dirPath = Paths.get(outputPath);

        // 随机生成文件夹数量
        int folderNum = random.nextInt(maxFolderCount) + 1;
        // 生成一个随机的布尔值,是否生成文件
        boolean isFile = random.nextBoolean();
        int fileNum = isFile ? random.nextInt(randomSum) + minSum : 0;
        createFolder(dirPath, 0, folderNum, isFile, fileNum);
    }

    /**
     * 创建文件夹
     *
     * @param dirPath     路径
     * @param folderDepth 深度
     * @param folderNum   文件夹数量
     * @param isFile      是否生成文件
     * @param fileNum     文件数量
     */
    public static void createFolder(Path dirPath, int folderDepth, int folderNum, boolean isFile, int fileNum) {
        try {
            // 创建输出目录
            Files.createDirectories(dirPath);
            ++folderDepth;
            for (int i = 1; i <= folderNum; i++) {
                // 不为最大深度时,往下递归生成文件夹,为最大深度时,不生成文件夹,必须生成文件
                if (folderDepth < folderMaxDepth) {
                    // 生成文件夹名
                    Path folderPath = randomFolderName(dirPath);
                    // // 随机生成文件夹数量
                    int folderNum1 = random.nextInt(maxFolderCount) + 1;
                    // 生成一个随机的布尔值,是否生成文件
                    boolean isFile1 = random.nextBoolean();
                    int fileNum1 = isFile1 ? random.nextInt(randomSum) + minSum : 0;
                    createFolder(folderPath, folderDepth, folderNum1, isFile1, fileNum1);
                } else if (folderDepth == folderMaxDepth) {
                    // 生成文件夹名
                    Path folderPath = randomFolderName(dirPath);
                    int fileNum1 = random.nextInt(randomSum) + minSum;
                    createFolder(folderPath, folderDepth, 0, true, fileNum1);
                }
            }

            if (isFile) {
                for (int i = 1; i <= fileNum; i++) {
                    //需要判断文件是否存在,如果存在,则重新生成文件名
                    Path filePath = randomFileName(dirPath);

                    // 创建文件
                    try (FileWriter writer = new FileWriter(filePath.toFile())) {
                        // 1MB的字符数组
                        char[] buffer = new char[charSize];
                        StringBuilder sb = new StringBuilder(buffer.length);
                        for (int x = 0; x < buffer.length; x++) {
                            // 填充字符
                            sb.append(fillChar);
                        }
                        String content = sb.toString();
                        // 转换为字节
                        long bytesToWrite = fileSize;
                        while (bytesToWrite > 0) {
                            writer.write(content);
                            bytesToWrite -= content.getBytes().length;
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 生成随机字符串
     *
     * @param length 字符串长度
     * @return 随机字符串
     */
    public static String randomString(int length) {
        String letters = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
        String result = "";

        Random random = new Random();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(letters.length());
            result += letters.charAt(index);
        }
        return result;
    }

    /**
     * 生成文件名,如果文件名已存在,则重新生成
     */
    public static Path randomFileName(Path dirPath) {
        Path filePath = dirPath.resolve(prefix + fileName + randomString(fileNameRandom) + suffix + "." + fileType);
        File file = filePath.toFile();
        if (file.exists()) {
            filePath = randomFileName(dirPath);
        }
        return filePath;
    }

    /**
     * 生成文件夹名,如果文件夹名已存在,则重新生成
     */
    public static Path randomFolderName(Path dirPath) {
        Path filePath = dirPath.resolve("folder_" + randomString(fileNameRandom));
        File file = filePath.toFile();
        if (file.exists()) {
            filePath = randomFolderName(dirPath);
        }
        return filePath;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值