4

This is code i Have written instead of editing a particular line new name gets appened at the last... please help me out....

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();

}
Bono
  • 183
  • 2
  • 14
user2198032
  • 51
  • 1
  • 1
  • 3

1 Answers1

7

Unless you aren't changing the (byte) length of the line, you need to rewrite the whole file, adding the changed line where appropriate. This is actually just a simple change from your current code. First, initialize your FileWriter without the append (since you don't want to just append to the end of the file, which is what you're doing now).

PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter("d:\\book.txt")));

Then, either read the whole file into memory (if the file is small enough) or else write a temp file as you go and then copy it over when you're done. The second way is more robust, and requires less code changing; just modify your while loop to write every line, modified or not.

// Open a temporary file to write to.
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter("d:\\book.temp")));

// ... then inside your loop ...

while ((line = br.readLine()) != null) {
    if (request.getParameter("hname").equals(line)) {
        line = line.replace(request.getParameter("hname"),
                request.getParameter("book"));
    }
    // Always write the line, whether you changed it or not.
    writer.println(line);
}

// ... and finally ...

File realName = new File("d:\\book.txt");
realName.delete(); // remove the old file
new File("d:\\book.temp").renameTo(realName); // Rename temp file

Don't forget to close all your file handles when you're done!

Henry Keiter
  • 16,863
  • 7
  • 51
  • 80
  • Java 7 update - you can let Java handle closing resources with the try-with-resources block. the Oracle tutorial for this is found at https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html – Jeutnarg Feb 22 '16 at 18:13
  • @Henry, is there a faster way to do this if you aren't changing the byte length of the file, eg: the contents that you are changing the line to are the same byte length? – Ofek Gila Sep 15 '16 at 06:34
  • @OfekGila If you know you're not changing the byte length, you can use a `RandomAccessFile` to seek in and simply overwrite any part of the file that you want. In my experience this tends to cause trouble down the road, though. – Henry Keiter Sep 16 '16 at 00:01