title: Remove a Specific Line From text file
body: <p>I trying to remove a specific line from a file. But I have  a problem in deleting a particular line from the text file. Let's said, my text file I want to remove Blueberry in the file following:</p>
  
  <p>Old List Text file:</p>
  
  <pre><code>Chocolate
  Strawberry
  Blueberry
  Mango
  </code></pre>
  
  <p>New List Text file:</p>
  
  <pre><code>Chocolate
  Strawberry
  Mango
  </code></pre>
  
  <p>I tried to run my Java program, when I input for delete and it didn't remove the line from the text file.</p>
  
  <p>Output:
  Please delete: 
  d
  Blueberry
  Remove:Blueberry</p>
  
  <p>When I open my text file, it keep on looping with the word "Blueberry" only. </p>
  
  <p>Text file:</p>
  
  <pre><code>Blueberry
  Blueberry
  Blueberry
  Blueberry
  Blueberry
  Blueberry
  Blueberry
  Blueberry
  </code></pre>
  
  <p>My question is how to delete the specific line from the text file? </p>
  
  <p>Here is my Java code:</p>
  
  <pre><code>String input="Please delete: ";
  System.out.println(input);
  
  try
          {        
              BufferedReader reader = new BufferedReader
                      (new InputStreamReader (System.in));
              line = reader.readLine();
  
              String inFile="list.txt";
              String line = "";
  
  
              while(!line.equals("x"))
              { 
                  switch(line)
                  {                   
  
                      case "d":
  
                      line = reader.readLine();
                      System.out.println("Remove: " + line);
                      String lineToRemove="";
  
                      FileWriter removeLine=new FileWriter(inFile);
                      BufferedWriter change=new BufferedWriter(removeLine);
                      PrintWriter replace=new PrintWriter(change);
  
                      while (line != null) {
                         if (!line.trim().equals(lineToRemove))
                         {
                               replace.println(line);
                               replace.flush();
                         }   
                      }
                      replace.close();
                      change.close();
                      break;
  
                  }
                  System.out.println(input);
                  line = reader.readLine();  
  
  
              }
  
          }
          catch(Exception e){
              System.out.println("Error!");
          }
  </code></pre>