创建指定大小、指定数量的文件
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){
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();
}
}
}
}