B
Buddha
Dear all,
Following is the code I wrote. I have a simple requirement. I need to
update a log file (txt file) everyday. For which, I initally take a
file.. and append it with some text. once it reaches a certain size. I
rename that file and append it with todays date. However, since the
file may reach the specified size at any given point. I take the
length of the file and append to the file name.. and decrement the
length. As of now, It is making only Two files. One the main file..
and second the renamed file.
The problem is for some reason the length gets stuck at 49. Then I
realise it is because the first file is being checked every time(I
check for the size).
Could some body please tell me how I could correct this code :
code:
--------------------------------------------------------------------------------
import java.io.BufferedWriter;import java.io.FileWriter;import
java.io.IOException;import java.text.*;import
java.util.Calendar;import java.util.Date; public class File1 { public
static void main(String[] args) { String nameRename; java.io.File
file; // Date d = new Date(); Calendar c = Calendar.getInstance();
String date1= new String(); //nameRename += d.getMonth); try
{ Date today = Calendar.getInstance().getTime();
DateFormat shortFormatter =
SimpleDateFormat.getDateInstance( SimpleDateFormat.SHORT ); //
create a long version date formatter DateFormat longFormatter =
SimpleDateFormat.getDateInstance( SimpleDateFormat.LONG ); date1 =
(String)longFormatter.format( today ); System.out.println("DATE 1 is
"+date1); file = new java.io.File("C://dblog"); // Create file if
it does not exist boolean exist = file.createNewFile(); if (!exist)
{ System.out.println("File already exists."); }else{
System.out.println("File created successfully."); } long length =
file.length(); if(length == 1 || length > 1){ int i = 0;
i=(int)length; // File (or directory) with new name
System.out.println("entering here"); file = new java.io.File(file
+date1+i); // Rename file (or directory) boolean success
= file.renameTo(file); if (success) {
System.out.println("Successfully renamed !");
file.createNewFile(); // File was not successfully
renamed } else{ System.out.println("renaming
failed"); } i--; } System.out.println("lenght is
"+length); BufferedWriter out = new BufferedWriter(new
FileWriter(file, true)); out.newLine(); /* uncomment this for non-
windows files*/ //out.write("\r"); out.write("testing for database
update transaciton logging"); out.close(); }catch (IOException e)
{ } catch(Exception e1){ } }
*********
P.S :
Yes I think, I have areason I cant use log4j for this.
Because AFAIK log4j doesnt give me any other logging level other than
the defaults ( debug,info,...,fatal ......for which am using log4j to
only log the warnings and fatals).
Where as this code, I will modify and have a static method being
called in my DAOs with params to append to this file.
I specifically need to be able to add the logs for updates or deletes,
so I have to write statements in my DAOs.Is there a simple way I can
include *this* functionality using Log4j, curently am using it for
logging application related stuff. I have done a lot of googling for
this.. but they are way too complex for me to follow. I am crushed
under deadlines . If you know of a better way or a solution to
this... I will be grateful.
Rgds
Following is the code I wrote. I have a simple requirement. I need to
update a log file (txt file) everyday. For which, I initally take a
file.. and append it with some text. once it reaches a certain size. I
rename that file and append it with todays date. However, since the
file may reach the specified size at any given point. I take the
length of the file and append to the file name.. and decrement the
length. As of now, It is making only Two files. One the main file..
and second the renamed file.
The problem is for some reason the length gets stuck at 49. Then I
realise it is because the first file is being checked every time(I
check for the size).
Could some body please tell me how I could correct this code :
code:
--------------------------------------------------------------------------------
import java.io.BufferedWriter;import java.io.FileWriter;import
java.io.IOException;import java.text.*;import
java.util.Calendar;import java.util.Date; public class File1 { public
static void main(String[] args) { String nameRename; java.io.File
file; // Date d = new Date(); Calendar c = Calendar.getInstance();
String date1= new String(); //nameRename += d.getMonth); try
{ Date today = Calendar.getInstance().getTime();
DateFormat shortFormatter =
SimpleDateFormat.getDateInstance( SimpleDateFormat.SHORT ); //
create a long version date formatter DateFormat longFormatter =
SimpleDateFormat.getDateInstance( SimpleDateFormat.LONG ); date1 =
(String)longFormatter.format( today ); System.out.println("DATE 1 is
"+date1); file = new java.io.File("C://dblog"); // Create file if
it does not exist boolean exist = file.createNewFile(); if (!exist)
{ System.out.println("File already exists."); }else{
System.out.println("File created successfully."); } long length =
file.length(); if(length == 1 || length > 1){ int i = 0;
i=(int)length; // File (or directory) with new name
System.out.println("entering here"); file = new java.io.File(file
+date1+i); // Rename file (or directory) boolean success
= file.renameTo(file); if (success) {
System.out.println("Successfully renamed !");
file.createNewFile(); // File was not successfully
renamed } else{ System.out.println("renaming
failed"); } i--; } System.out.println("lenght is
"+length); BufferedWriter out = new BufferedWriter(new
FileWriter(file, true)); out.newLine(); /* uncomment this for non-
windows files*/ //out.write("\r"); out.write("testing for database
update transaciton logging"); out.close(); }catch (IOException e)
{ } catch(Exception e1){ } }
*********
P.S :
Yes I think, I have areason I cant use log4j for this.
Because AFAIK log4j doesnt give me any other logging level other than
the defaults ( debug,info,...,fatal ......for which am using log4j to
only log the warnings and fatals).
Where as this code, I will modify and have a static method being
called in my DAOs with params to append to this file.
I specifically need to be able to add the logs for updates or deletes,
so I have to write statements in my DAOs.Is there a simple way I can
include *this* functionality using Log4j, curently am using it for
logging application related stuff. I have done a lot of googling for
this.. but they are way too complex for me to follow. I am crushed
under deadlines . If you know of a better way or a solution to
this... I will be grateful.
Rgds