-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployee_Update.java
More file actions
57 lines (46 loc) · 1.63 KB
/
Employee_Update.java
File metadata and controls
57 lines (46 loc) · 1.63 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
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
class Employee_Update implements FileFunctions {
@Override
public void createFileBaseHTML() throws IOException {
// Not implemented
}
@Override
public void listUserdataFiles() throws IOException {
// Not implemented
}
@Override
public void removeFile(String id) throws IOException {
// Not implemented
}
@Override
public void viewFile(String id) throws IOException {
// Not implemented
}
public void updateFile(String id, String oldData, String newData) {
File file = new File(FileFunctions.USER_DATA_DIR + "employee" + id + ".html");
if (!file.exists()) {
System.out.println("Employee record not found.");
return;
}
try (Scanner sc = new Scanner(file);
FileWriter writer = new FileWriter(file)) {
StringBuilder fileContent = new StringBuilder();
while (sc.hasNextLine()) {
fileContent.append(sc.nextLine()).append("\n");
}
String content = fileContent.toString();
if (!content.contains(oldData)) {
System.out.println("The old data was not found in the file.");
return;
}
String updatedContent = content.replace(oldData, newData);
writer.write(updatedContent);
System.out.println("Employee record has been updated successfully!");
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}