-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathWriter.java
More file actions
61 lines (53 loc) · 2.14 KB
/
Writer.java
File metadata and controls
61 lines (53 loc) · 2.14 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package com.engflow.varyinginputs.writer;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;
public class Writer {
/**
* Write the specified number of files to the output path.
* @param numberOfFiles
* @param outputPath
*/
public void writeFiles(int numberOfFiles, String outputPath) {
File directory = new File(outputPath);
if (!directory.exists()) {
directory.mkdirs();
}
// list all files in the directory and count them
File[] existingFiles = directory.listFiles();
int existingFilesCount = existingFiles != null ? existingFiles.length : 0;
Random random = new Random();
// create new files if the number of existing files is less than the required number
if (existingFilesCount < numberOfFiles) {
for (int i = existingFilesCount; i < numberOfFiles; i++) {
// create a new file with a unique name to avoid conflicts
String fileName = outputPath + "/file" + UUID.randomUUID() + ".txt";
File file = new File(fileName);
try {
// write 100 random characters to the file
file.createNewFile();
FileWriter writer = new FileWriter(file);
for (int j = 0; j < 100; j++) {
char randomChar = (char) (random.nextInt(26) + 'a');
writer.write(randomChar);
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// delete files if the number of existing files is greater than the required number
else if (existingFilesCount > numberOfFiles) {
while (existingFilesCount > numberOfFiles) {
int fileIndex = random.nextInt(existingFilesCount);
File fileToDelete = existingFiles[fileIndex];
if (fileToDelete.delete()) {
existingFilesCount--;
}
}
}
}
}