title: I want to open a text file and edit a specific line in java
body: <p>This is code i Have written instead of editing a particular line new name gets appened at the last...
  please help me out....</p>
  
  <pre><code>PrintWriter writer = new PrintWriter(new BufferedWriter(
          new FileWriter("d:\\book.txt", true)));
  
  BufferedReader br = null;
  FileReader reader = null;
  try {
      reader = new FileReader("d:\\book.txt");
      br = new BufferedReader(reader);
      String line;
      System.out.println((";;;;;;;;;;;;;;;;" + request
              .getParameter("hname")));
      System.out.println(request.getParameter("book"));
      while ((line = br.readLine()) != null) {
  
          if (request.getParameter("hname").equals(line)) {
              line = line.replace(request.getParameter("hname"),
                      request.getParameter("book"));
  
              writer.println(line);
  
              writer.close();
          }
      }
  
  } catch (FileNotFoundException e) {
      e.printStackTrace();
  }finally{
      reader.close();
  
  }
  </code></pre>