漂流的老妖怪

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
统计
 
创建指定大小、指定数量的文件
复制代码
import java.io.*;
import java.util.UUID;

/**
 * @Version : 1.0
 * @Author : lihao
 * @Date : 2022/6/13 11:49
 **/
public class Test22 {

    /**
     * 创建指定大小、指定数量的文件
     */
    public static void cFile(){
        String storePath = "D:/data/fileout/";  //文件存储目录
        int cFileSize = 10; //输出文件大小,单位KB
        int cFileSNum = 10; //输出文件数量

        StringBuilder strb = new StringBuilder();
        for(int i=0; i<1024*cFileSize; i++){
            strb.append('a'+"");
        }
        String strContent = strb.toString();  //文件输出内容

        for(int i=0;i<cFileSNum;i++){
            String fileOutPath = storePath + UUID.randomUUID().toString() + ".txt";  //文件路径,如:D:/data/tt/3e04c1.txt

            //文件数据写入(如果文件夹和文件不存在,则先创建,再写入)
            String filedo = "write";
            FileWriter fw = null;
            try {
                File file = new File(fileOutPath);
                //如果文件夹不存在,则创建文件夹
                if (!file.getParentFile().exists()) {
                    file.getParentFile().mkdirs();
                }
                if (!file.exists()) {//如果文件不存在,则创建文件,写入第一行内容
                    file.createNewFile();
                    fw = new FileWriter(file);
                    filedo = "create";
                } else {//如果文件存在,则追加内容;
                    fw = new FileWriter(file, true);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            PrintWriter pw = new PrintWriter(fw);
            pw.println(strContent);
            pw.flush();
            try {
                fw.flush();
                pw.close();
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
复制代码

 

posted on   漂流的老妖怪  阅读(101)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
 
点击右上角即可分享
微信分享提示