-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuiremovecollection.java
More file actions
39 lines (34 loc) · 1.32 KB
/
Copy pathuiremovecollection.java
File metadata and controls
39 lines (34 loc) · 1.32 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
public class uiremovecollection {
public static RemoveCollectionEvent() {}
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();
}
}