0

Here's my code that creates a log file:

// Start a log file
FileHandler fh;
String logFileName = "";
try {  
    SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy_HHmmss");
    logFileName = "folder/mylog_" + format.format(Calendar.getInstance().getTime()) + ".log";
    fh = new FileHandler(logFileName);  
    fh.setFormatter(new SimpleFormatter());  
    logger.addHandler(fh);
    logger.setUseParentHandlers(false);
} catch (SecurityException e) {  
    logger.log(Level.SEVERE, "Exception: ", e);
} catch (IOException e) {  
    logger.log(Level.SEVERE, "Exception: ", e);  
}  

Later in my class I need to be able to delete this log file either based on "logFileName" path or the "FileHandler fh".

I tried this "logger.removeHandler(fh);" but it doesn't seem to be working. How can I do that exactly?

goe
  • 1,153
  • 2
  • 12
  • 24
  • 1
    To delete a file, you create a `File` object that corresponds to that file, then call `file.delete()`. There are many examples on how to create a `File` object on here. – Ascalonian Feb 03 '15 at 13:05
  • 1
    Take a look at this post for an example http://www.programcreek.com/java-api-examples/index.php?api=java.util.logging.FileHandler – Kenneth Clark Feb 03 '15 at 13:06
  • Always a good place to check. The Oracle Java turorials `http://docs.oracle.com/javase/tutorial/essential/io/delete.html` – SubOptimal Feb 03 '15 at 13:07

0 Answers0