【Java文件管理】批量生成文件,按时间格式路径生成

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.*;

/**
 * @author yhc
 * @description 批量生成文件
 */
public class BatchCreateFile {
   
    /**
     * 文件路径
     */
    public static final String path = "/home/cathay10/workSpace/log";
    /**
     * 随机数生成器
     */
    static Random random = new Random();
    /**
     * 最小文件数量
     */
    static int minSum = 5;
    /**
     * 随机文件数量,最大为minSum+randomSum
     */
    static int randomSum = 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值