Korenron
Korenron

Reputation: 101

java add new line to txt\log file

I'm trying to create a log file to my java script that will save the time when the unit is up.

it's working - every boot he save me the time in the file but I want to add a new line every boot , and now it overwrite the file so the file is allways 1 line (the last time it boot)

what do I need to change?

this is the code I have wrote :

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;
import java.text.SimpleDateFormat;
import java.util.Scanner;

public class HelloWorld {
    public static void main(String[] args) throws Exception 
    {

        Writer output; //to write the data into a file 
        String PathOfFile = "/home/pi/boot.log";
        PrintWriter writer = new PrintWriter(PathOfFile);
        output = new BufferedWriter(new FileWriter(PathOfFile , true));
        Calendar Time = Calendar.getInstance(); //get the time 
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy-HH:mm:ss");

        try 
        {
            System.out.println("Hello - the device is UP and running now :-)");

            output.append ("\r\n" + sdf.format(Time.getTime()) + "   Unit is UP\r\n " );
            output.close();

            System.exit(0);
        }

        catch (Exception e)
        {
            System.err.println(e);
            output.append (sdf.format(Time.getTime()) +  "    " + e );
            output.close();

        }
    }

}

Upvotes: 1

Views: 118

Answers (2)

Eby Jacob
Eby Jacob

Reputation: 1458

PrintWriter's method is called append() doesn't mean that it changes the mode of the file being opened.

You need to open the file in append mode as well:

Also, you are not using printwriter in your code, so this can be removed. Try this

import java.io.*;
import java.net.*;
import java.util.*;
import java.util.Calendar;
import java.util.concurrent.TimeUnit;
import java.text.SimpleDateFormat;
import java.util.Scanner;

public class HelloWorld {
    public static void main(String[] args) throws Exception 
    {

        Writer output; //to write the data into a file 
        String PathOfFile = "/home/pi/boot.log";
        //PrintWriter writer = new PrintWriter(PathOfFile , true /* append = true */)
        output = new BufferedWriter(new FileWriter(PathOfFile , true));
        Calendar Time = Calendar.getInstance(); //get the time 
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy-HH:mm:ss");
        try 
        {
            System.out.println("Hello - the device is UP and running now :-)");
            output.append ("\r\n" + sdf.format(Time.getTime()) + "   Unit is UP\r\n " );
            output.close();

            System.exit(0);
        }

        catch (Exception e)
        {
            System.err.println(e);
            output.append (sdf.format(Time.getTime()) +  "    " + e );
            output.close();
        }
    }
}

Upvotes: 2

hradecek
hradecek

Reputation: 2513

The problem is that you are opening file with PrintWriter instance as well, not in append mode, which removes the content and after that you are writing with Writer (output) to the file.

Solution:

Just remove the PrintWriter writer = new PrintWriter(PathOfFile);, you are not using it anyway in your code.

Upvotes: 0

Related Questions