-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveCollectionEvent.java
More file actions
49 lines (40 loc) · 1.56 KB
/
Copy pathRemoveCollectionEvent.java
File metadata and controls
49 lines (40 loc) · 1.56 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
import java.io.*;
import java.util.Scanner;
public class RemoveCollectionEvent {
public static void removeCollectionEvent() {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the ID of the collection to be removed: ");
String collectionId = scanner.nextLine();
File myFile = new File("Collectiondatabase.txt");
File tempFile = new File("temp.txt");
try (BufferedReader reader = new BufferedReader(new FileReader(myFile));
FileWriter writer = new FileWriter(tempFile)) {
String lineToRemove = collectionId;
String currentLine;
boolean found = false;
while ((currentLine = reader.readLine()) != null) {
if (currentLine.startsWith(lineToRemove)) {
found = true;
continue;
} else {
writer.write(currentLine + "\n"); // changed to "\n"
}
}
if (!found) {
System.out.println("Collection not found.");
} else {
System.out.println("Collection removed successfully.");
}
writer.close();
reader.close();
if (!myFile.delete()) {
System.out.println("Could not delete the original file.");
}
if (!tempFile.renameTo(myFile)) {
System.out.println("Could not rename the temporary file.");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}