Basically I have this code.. I want to be able to display the delete a line in the text file ..
Example:
Gab 223-2587
Mark 221-5678
Reca 227-7589
Feb 224-8597
Command:
remove Mark
The result in the text file should be:
Gab 223-2587
Reca 227-7589
Feb 224-8597
My problem is, it doesn't remove the line that I want to remove.. and the text file becomes empty.. Or if i add a contact and then attempt to remove an existing contact.. the file will only contain the recently added contact..
Here's my code:
File inputFile = new File("C:/Users/Gab Real/workspace/CSc121/MyContacts.txt");
System.out.println(String.format("File.canWrite() says %s", inputFile.canWrite()));
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
Scanner scan = new Scanner(System.in);
String word = "";
String currentline = " ";
StringBuilder fileOutput = new StringBuilder();
while((currentline = reader.readLine()) != null) {
fileOutput.append(currentline + "\r\n");
}
PrintWriter out = new PrintWriter(inputFile);
out.println(fileOutput.toString());
if(word.startsWith("add")){
out.append(entries[1] + " " + entries[2] + "\r\n");
//out.println(out.toString());
//out.write("\r\n");
}
//my if condition to remove a line
else if (word.contains("remove")){
String currentLine;
while((currentLine = reader.readLine()) != null) {
// trim newline when comparing with lineToRemove
String trimmedLine = currentLine.trim();
if(trimmedLine.contains(entries[1])) continue;
out.append(currentLine + System.getProperty("line.separator"));
}