-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRandomFiles.java
More file actions
106 lines (86 loc) · 3.25 KB
/
RandomFiles.java
File metadata and controls
106 lines (86 loc) · 3.25 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import java.io.File;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class RandomFiles {
public static File randomFiles = new File("randomFiles");
public static void randomFileMaker(int numberofFiles) {
if (!randomFiles.exists()) {
randomFiles.mkdir();
}
Random rand = new Random();
String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (int i = 1; i <= numberofFiles; i++) {
try {
int length = rand.nextInt(41) + 100; // length of text
StringBuilder sb = new StringBuilder(length);
for (int j = 0; j < length; j++) {
sb.append(chars.charAt(rand.nextInt(chars.length())));
}
String str = sb.toString();
String fileName = "file" + i + ".txt";
File newFile = new File(randomFiles.getPath(), fileName);
Files.write(newFile.toPath(), str.getBytes(StandardCharsets.UTF_8),
StandardOpenOption.CREATE);
// System.out.println("Created " + fileName + " with content: " + str);
} catch (Exception e) {
System.err.println("Error creating random text file #" + i);
}
}
}
public static void deleteRandomFileMaker() {
File[] files = randomFiles.listFiles();
if (files != null) {
for (File file : files) {
if (!file.delete()) {
System.out.println("Failed to delete: " + file.getPath());
}
}
}
randomFiles.delete();
}
// https://stackoverflow.com/questions/7899525/how-to-split-a-string-by-space
public static boolean FileMakerChecker() {
try {
List<String> lines = Files.readAllLines(GitDirectory.index.toPath());
List<String> hashes = new ArrayList<>();
List<String> fileNames = new ArrayList<>();
for (String line : lines) {
String[] parts = line.split(" ", 2);
hashes.add(parts[0]);
fileNames.add(parts[1]);
}
// checking blobs
for (String hash : hashes) {
if (Git.BlobChecker(hash) == false) {
// System.out.println(hash + " is not found");
return false;
}
}
// checking Files
for (String fileName : fileNames) {
if (randomFilesChecker(fileName) == false) {
// System.out.println(fileName + " is not found");
return false;
}
}
return true;
} catch (Exception e) {
System.out.println("Error reading file");
}
return false;
}
public static boolean randomFilesChecker(String fileName) {
File[] files = randomFiles.listFiles();
if (files != null) {
for (File file : files) {
if (file.getName().equals(fileName)) {
return true;
}
}
}
return false;
}
}