1. 在指定文件夹底下随机生成文件夹及文件
1.1 参数
参数名 | 含义 | 默认值 |
---|
baseDir | 文件根路径 | /home/yhc/workSpace/log |
minSum | 最小文件数量 | 10 |
randomSum | 随机文件数量,最大为minSum+randomSum | 10 |
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;
public class BatchCreateFileDepth {
public static final String baseDir = "/home/cathay10/workSpace/log";
static Random random = new Random();
static int minSum = 5;
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;
static int charSize = 1 * 1024;
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);
}
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())) {
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();
}
}
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;
}
}