Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to append the log file on each time of execution.
How to do this using fallowing code...

What I have tried:

I'm writing log file using this code.....

Java
static String fileTempPath= System.getProperty("java.io.tmpdir");
	static Date ch=new java.sql.Date(System.currentTimeMillis());
	static File tempFile=new File(fileTempPath+"PurgeLog"+ch  +  ".log");
		static PrintWriter out;
    public static void main(String[] args)                             
    {
				out=new PrintWriter(tempFile);
				out.println("main started:")
			 out.println("heloooo:")
out.close();
			 
    }
	}
Posted
Updated 10-Apr-17 0:58am
v3

1 solution

Use the FileWriter (Java Platform SE 7 )[^] class and the constructor that accepts an append parameter:
Java
FileWriter out = new FileWriter(fileTempPath+"PurgeLog"+ch + ".log", true);
out.write("main started:\n");
out.write("heloooo:\n");
out.close();
 
Share this answer
 
Comments
CPallini 10-Apr-17 6:57am    
5.
Member 13012003 12-Apr-17 0:19am    
It is working fine...but how to prepend??? i.e latest execution logs should come first..
Jochen Arndt 12-Apr-17 2:55am    
That is rather uncommon and is not so simple as appending.
A possible solution:
- Create a new temporaray file
- Write data to that file
- Open log file, read content (e.g. line by line) and write to the temp file
- Close temporary file
- Delete log file
- Rename temporary file to log file

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900