-2

I am working on a log file that is currently over-writing itself every single time. Now The thing is all i want it to do is to just write on the first line and append the list below to show the history from newest to oldest. Problem is I am not sure how to go about it I am looking at the code but don't know what I am doing wrong. Here is the code not sure if I am missing something or not.

 String historylog = new Date().toString();

    BufferedWriter bw = null;

    String filepath = "C:\netbeans\Source code\test3";
    String filename = "PatchHistory.log";

    try
    {
        if (!(new File( filepath).exists()))
            (new File( filepath)).mkdirs();
        bw = new BufferedWriter( new FileWriter( filepath + File.separator
       + filename, true));

        bw.write( historylog + "\r\n");
        bw.newLine();
        bw.flush();
        bw.close();
        return true;
    }

    catch (IOException){

    return false;
    }

Any Help would be appreciated not sure what I am doing wrong with this.

M1M N7
  • 69
  • 2
  • 7

2 Answers2

2

If I have understood you, you want to add log entries at the beginning of a log file.

AFAIK, (if you know any exceptions please tell me) all filesystems add data to the end of file. And Direct Access would overwrite the beginning of the file.

Your best option would be writting the log to a different file and, after writting what you want, write after that the contents of the original log file. Once done, close both files and overwrite the old file with the new one.

SJuan76
  • 24,532
  • 6
  • 47
  • 87
  • +1 Good catch. But I'd still be tempted to use append on the core log file (and load the file and reverse the output to a 2nd file if needed). Good catch on the other matter a well. :P – Andrew Thompson Dec 27 '12 at 18:24
  • If it does it at the end as well i don't mind the biggest problem I am having is just that it is over-writing the log file. If i can prevent that I would be happy to atleast have that. – M1M N7 Dec 27 '12 at 18:41
  • AFAIK, your code (and Andrew Thompson answer did agree with mine) is correct for adding to the end of the file, it should not be overwritten. Cannot offer more help other than checking that the file is not deleted **before** this snippet of code (hint1: Check if the file exists before opening it and its size, write that to STDOUT or to the log itselfe; hint2: write your code as a standalone application that does nothing else that write a few lines and check if the behavior reproduces). – SJuan76 Dec 27 '12 at 19:17
  • okay thank you seems that way. gotta check to see what else is changing. – M1M N7 Dec 27 '12 at 20:25
1

In your code

You are wrong here, you didn't used if statement properly.

 if (!(new File( filepath).exists()))
        (new File( filepath)).mkdirs(); // file path not exist, then it will execute
    bw = new BufferedWriter( new FileWriter( filepath + File.separator + filename, true)); // this append file will always execute

Solution

if (!(f.exists())) {
//create new file
bw = new BufferedWriter(new FileWriter(filepath + File.separator + filename, false));
}
else{
//append in existing file
bw = new BufferedWriter(new FileWriter(filepath + File.separator + filename,true));
}
Ravi
  • 30,829
  • 42
  • 119
  • 173
  • So which is more accurate the updated one below or the one above the update? Dont know how to read this var they both seem similar – M1M N7 Dec 27 '12 at 18:43
  • it depends what you want to do ?? I really didn't get, what you exatly trying there ?? – Ravi Dec 27 '12 at 18:44
  • oh i am trying to prevent the log file from over-writing itself. I want to use the same file if its there without erasing the data in it. and add more data to it – M1M N7 Dec 27 '12 at 18:46
  • In update section, i pointed your mistake and above update, this is my solution. So, i will say use first one. Which will create file, if it is not there and by the next time it will appen into existing one. :) – Ravi Dec 27 '12 at 18:47
  • thank you mate!, will try it out and see the results :) does that mean i dont need the "(new File( filepath)).mkdirs();" – M1M N7 Dec 27 '12 at 18:52
  • are you sure your code is working fine. It seems you have used wrong logic. – Ravi Dec 27 '12 at 18:53
  • Nevertheless, you just try and let me know. – Ravi Dec 27 '12 at 18:54
  • I dont know what you mean by that I know the logic is wrong being that it doesn't do what i would like it to do. – M1M N7 Dec 27 '12 at 18:55
  • No issue, just try and let me know – Ravi Dec 27 '12 at 18:56