when terminated the program with Control + c, how to close all open file streams?

ahmet oguz

Greenhorn

Posts: 18

posted 9 years ago

Hi, I write a very simple program that create a dummy log file. I have a BufferedWriter object and writes dummy line to log file. I run this program on linux on command line and when i press the control + c, my open bufferedWriter stream is not closed. I close the BufferedWriter object on finally block, but if i press the control +c, this block is not run. How do i close this stream also when terminted the program with Control+c My code=

System.addShutdownHook should do the trick. From experience, any thread registered using this method will be run even if you interrupt the program (either by pressing CTRL+C or by closing the program from task manager's first tab). The only way they too will be skipped is by really killing the process, from the task manager's second tab (processes).

ahmet: How do i close this stream also when terminted the program with Control+c

Apart from academic purpose, I dont think there is any need of doing this. Whenever a process is terminated/killed(CTRL-C), the file descriptors are released. You really do not need to close the stream in such cases.