1. 在指定文件夹底下随机生成文件夹及文件
1.1 参数
参数名 | 含义 | 默认值 |
---|
path | 生成文件夹路径 | /home/cathay10/workSpace/log |
minSum | 随机数生成器 | 20 |
randomSum | 随机文件数量,最大为minSum+randomSum | 10 |
startDate | 指定开始日期,为空则默认1970-01-01开始 | 2021/01/01 |
numberOfDates | 生成的日期数量 | 9 |
plusDays | 日期偏移量,输入0为当前日期,正数为当前日期后n天,负数为当前日期前n天 | 0 |
prefix | 文件名前缀 | log_ |
suffix | 文件名后缀 | _log |
fileType | 文件类型 | _log |
fileName | 文件名 | file |
fileNameRandom | 文件名随机数长度(含数字、字母) | 6 |
charSize | 设置数组大小为1MB,可以根据需要调整 | 10 * 1024 * 1024 |
fileSize | 设置文件大小为1MB,可以根据需要调整 | 10 * 1024 * 1024 |
fillChar | 填充字符,用于填充文件大小 | x |
生成的文件夹格式/ /home/cathay10/workSpace/log/yyyy/MM/dd/ | | |
1.2 代码
package com.api.apidemo.tool.file;
import org.apache.commons.lang3.StringUtils;
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.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
public class BatchCreateFile {
public static final String path = "/home/cathay10/workSpace/log";
static Random random = new Random();
static int minSum = 5;
static int randomSum = 5;
static int numberOfDates = 1500;
static int plusDays = 0;
static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
static String startDate = "2000/01/01";
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 * 1024;
static int fileSize = 1 * 1024 * 1024;
static char fillChar = 'x';
public static void main(String[] args) {
batchCreateFile();
}
public static void batchCreateFile() {
Set<LocalDate> uniqueDates = generateRandomPastDate(numberOfDates, plusDays);
uniqueDates.forEach(date -> createFile(date, randomSum == 0 ? minSum : random.nextInt(randomSum) + minSum));
}
private static void createFile(LocalDate currentDate, int numberOfFiles) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
String formattedDate = currentDate.format(formatter);
String outputPath = path + "/" + formattedDate;
Path dirPath = Paths.get(outputPath);
try {
Files.createDirectories(dirPath);
for (int i = 1; i <= numberOfFiles; 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 Set<LocalDate> generateRandomPastDate(int numberOfDates, int plusDays) {
Set<LocalDate> uniqueDates = new HashSet<>();
while (uniqueDates.size() < numberOfDates) {
if (StringUtils.isEmpty(startDate)) {
startDate = "1970/01/01";
}
LocalDate startDates = LocalDate.parse(startDate, formatter);
LocalDate today = LocalDate.now().plusDays(plusDays);
long randomDays = random.longs(startDates.toEpochDay(), today.toEpochDay()).findFirst().getAsLong();
LocalDate date = LocalDate.ofEpochDay(randomDays);
uniqueDates.add(date);
}
return uniqueDates;
}
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;
}
}