-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileCreator.java
More file actions
138 lines (124 loc) · 5.93 KB
/
Copy pathFileCreator.java
File metadata and controls
138 lines (124 loc) · 5.93 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package dashboard;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
//This class creates all the files required to run the code without any errors, if they don't exist:
public class FileCreator {
private static final String FILE_SEPARATOR = System.getProperty("file.separator");
private static final String FILES_FOLDER = "files";
public static void go() {
createFolderIfNotExists();
// List of file names to check and create if not exist
String[] fileNames = {
"announcements.txt",
"assignments.txt",
"errors.csv",
"integration.csv",
"intstats.csv",
"monthstats.csv",
"statuses.csv",
"users.csv"
};
// Check and create files
for (String fileName : fileNames) {
createFileIfNotExists(fileName);
}
System.out.println("Files created or already exist.");
}
private static void createFolderIfNotExists() {
String path = null;
try {
path = new File(".").getCanonicalPath();
} catch (IOException e) {
System.out.println("Error: while getting the path of the program.");
}
String folderPath = path + FILE_SEPARATOR + "dashboard" + FILE_SEPARATOR + FILES_FOLDER;
File folder = new File(folderPath);
if (!folder.exists() && !folder.isDirectory()) {
if (folder.mkdirs()) {
System.out.println("Folder created: " + folderPath);
} else {
System.out.println("Error creating folder: " + folderPath);
}
}
}
private static void createFileIfNotExists(String fileName) {
try {
String path = null;
try {
path = new File(".").getCanonicalPath();
} catch (IOException e) {
System.out.println("Error: while getting the path of the program.");
}
String filePath = path + FILE_SEPARATOR + "dashboard" + FILE_SEPARATOR + FILES_FOLDER + FILE_SEPARATOR + fileName;
File file = new File(filePath);
if (file.createNewFile()) {
System.out.println("File created: " + filePath);
initializeFileContent(file, fileName);
} else {
System.out.println("File already exists: " + filePath);
}
} catch (IOException e) {
System.out.println("Error creating file: " + e.getMessage());
}
}
//All the default file contennts:
private static void initializeFileContent(File file, String fileName) {
try (FileWriter writer = new FileWriter(file)) {
switch (fileName) {
case "announcements.txt":
writer.write("- Server Update: Servers will be offline from 12pm to 1pm.");
break;
case "assignments.txt":
writer.write("- Audit solved errors\n- Meeting: Discuss Employee benefits");
break;
case "errors.csv":
writer.write("ErrorName,ServerID,Status,LastUpdateTime\n");
writer.write("Database Connection,1,In Progress,30-10-2023 08:15\n");
writer.write("Disk Space Full,2,Automatic,30-10-2023 08:16\n");
writer.write("CPU Overload,1,Not Opened,30-10-2023 08:17\n");
writer.write("Network Disruption,3,Fixed,30-10-2023 08:18");
break;
case "integration.csv":
writer.write("PipelineName,Environment,Branch,Status,LastRunTimestamp\n");
writer.write("SQL-Analyze-Dev,Dev,Feature-123,Success,30-10-2023 08:00\n");
writer.write("SQL-Deploy-Dev,Dev,Main,Success,30-10-2023 08:05\n");
writer.write("SQL-Test-Dev,Dev,Feature-124,Failed,30-10-2023 08:10\n");
writer.write("SQL-Analyze-Prod,Production,Main,Success,30-10-2023 08:15");
break;
case "intstats.csv":
writer.write("MonthYear,Deployments\n");
writer.write("Oct-22,8\n");
writer.write("Nov-22,12\n");
writer.write("Dec-22,15\n");
writer.write("Jan-23,10");
break;
case "monthstats.csv":
writer.write("ServerName,ErrorCount,QueriesPerMonth\n");
writer.write("Server A,2,1500\n");
writer.write("Server B,2,2000\n");
writer.write("Server C,1,1000\n");
writer.write("Server D,3,1800");
break;
case "statuses.csv":
writer.write("ServerName,ServerAddress,CPUUsage,Cycles,IO,LastUpdate,ServerHealth\n");
writer.write("Server A,192.168.1.101,30%,5000,4GB,30-10-2023 08:00,Good\n");
writer.write("Server B,192.168.1.102,20%,4000,8GB,30-10-2023 08:01,Fair\n");
writer.write("Server C,192.168.1.103,40%,6000,2GB,30-10-2023 08:02,Excellent\n");
writer.write("Server D,192.168.1.104,25%,4500,6GB,30-10-2023 08:03,Good");
break;
case "users.csv":
writer.write("ID,UserEmail,Password,Name,Designation\n");
writer.write("000,admin@sql,0000,Admin,admin\n");
writer.write("001,eng1@sql,pass1,Mr.Eng,engineer\n");
writer.write("002,eng2@sql,pass2,Ms.Eng,engineer");
break;
default:
// For unknown files, write an informative message
writer.write("File content not initialized for " + fileName);
}
} catch (IOException e) {
System.out.println("Error initializing file content: " + e.getMessage());
}
}
}