My program (a server) has a logger, it is an attribute of the main server class and it is used by all the other classes to log an error. Is there a way to delete the .lck files when the program terminates? My code:
private static Logger log = Logger.getLogger("ServerLog");
public Server() {
// initialize variables
try {
FileHandler fh = new FileHandler(
"src/main/resources/log/ServerLog.log", true);
log.addHandler(fh);
log.setLevel(Level.ALL);
fh.setFormatter(new SimpleFormatter());
} catch (SecurityException e) {
getLog().log(Level.WARNING, e.getMessage());
} catch (IOException e) {
getLog().log(Level.WARNING, e.getMessage());
}
}
And it is called by other classes to log errors.
Second question: when I write on a file like:
public final void saveRanking() {
try {
FileOutputStream fos = new FileOutputStream(
"src/main/resources/database/dataRanking.out");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(getRanking());
oos.flush();
oos.close();
fos.close();
} catch (FileNotFoundException e) {
getLog().log(Level.SEVERE, e.getMessage());
} catch (IOException e) {
getLog().log(Level.SEVERE, e.getMessage());
}
}
is there a way to ensure that the file is saved without problem even if the process terminates during the writing?