-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileExplorer.java
More file actions
132 lines (110 loc) · 3.84 KB
/
FileExplorer.java
File metadata and controls
132 lines (110 loc) · 3.84 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
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;
public class FileExplorer {
private Set<Path> files = new HashSet<>();
AuditReport report = new AuditReport();
boolean canRead(Path file) throws IOException {
if (Files.isHidden(file)) return false;
if (!Files.isRegularFile(file)) return false;
String name = file.toString().toLowerCase();
return name.endsWith(".txt")
|| name.endsWith(".log")
|| name.endsWith(".java")
|| name.endsWith(".html");
}
String findType(String text) {
if (hasEmail(text)) return "EMAIL";
if (hasCard(text)) return "CREDIT_CARD";
if (hasPhone(text)) return "PHONE";
return "NORMAL";
}
boolean hasEmail(String text) {
int pos = text.indexOf('@');
return pos > 0 && text.indexOf('.', pos) > pos;
}
boolean hasPhone(String text) {
return digitCount(text) == 10;
}
boolean hasCard(String text) {
int d = digitCount(text);
return d >= 13 && d <= 16;
}
int digitCount(String text) {
int c = 0;
for (char ch : text.toCharArray()) {
if (Character.isDigit(ch)) c++;
}
return c;
}
void readFile(Path file) throws IOException {
List<String> lines = Files.readAllLines(file);
if (lines.isEmpty()) return;
files.add(file);
for (int i = 0; i < lines.size(); i++) {
String text = lines.get(i);
String type = findType(text);
String level = type.equals("NORMAL") ? "INFO" : "ALERT";
FileRecord rec = new FileRecord(
file,
i + 1,
type,
level,
text
);
report.addRecord(rec);
}
}
public void execute(Path root) {
try {
Scanner menu = new Scanner(System.in);
Files.walk(root).forEach(p -> {
try {
if (canRead(p)) {
System.out.println("Inspecting file: " + p);
readFile(p);
}
} catch (IOException e) {
System.out.println("Skipped file: " + p);
}
});
report.printOverview();
report.printDetails();
boolean run = true;
while (run) {
System.out.println("\nChoose operation:");
System.out.println("(1) Save audit log\n(2) Remove inspected files\n(3) Exit");
int opt = menu.nextInt();
switch (opt) {
case 1:
System.out.print("Confirm save? (Y/n): ");
if (menu.next().equalsIgnoreCase("y")) {
report.saveToFile();
System.out.println("Audit log saved successfully.");
}
break;
case 2:
System.out.print("Confirm delete? (Y/n): ");
if (menu.next().equalsIgnoreCase("y")) {
for (Path f : files) {
FileRemover.remove(f);
}
System.out.println("All inspected files removed.");
}
break;
case 3:
run = false;
break;
default:
System.out.println("Invalid selection.");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
}