Search Your Question

Tuesday, May 19, 2009

Save exception in a log file.

//This is a static function to save the exception in a file.

public static boolean writeToFile(String text)
{
Date recentdate=Calendar.getInstance().getTime();
logger=new File("C:\\File_Name.txt");
try {
FileWriter fw = new FileWriter(logger, true);
fw.write("\n"+recentdate+": "+text);
fw.flush();
fw.close();
} catch (IOException ex) {
JOptionPane.showMessageDialog(null, ex);
return false;
}

return true;
}

//This is the process to save the exception

ExceptionLog.writeToFile(this.getClass().getName()+": "+e);