aspose file tools
The moose likes Java in General and the fly likes Is there any way to close all existing file handlers when the java application ends? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Is there any way to close all existing file handlers when the java application ends?" Watch "Is there any way to close all existing file handlers when the java application ends?" New topic
Author

Is there any way to close all existing file handlers when the java application ends?

cle tan
Ranch Hand

Joined: Jun 11, 2012
Posts: 68
As sometimes when the application ends, some of the logs will be xxx.log.1 , xxx.log.2
seems like the logs are still being locked.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32827
    
    4
You would usually close files immediately after reading or writing, but I see that log files would stay open until the JVM exits.
Can you list all open files?
What about a shutdown hook which starts a new thread to close all those files?
cle tan
Ranch Hand

Joined: Jun 11, 2012
Posts: 68
Assuming java application program ended, can it guarantee that a file won't be locked when it exits?

how to implement a shutdown hook?
Ivan Jozsef Balazs
Ranch Hand

Joined: May 22, 2012
Posts: 445
    
    1
Any decent operating system will free the resources held by a program upon its termination.
Exactly what kind of lock is it? Does it persist across the life span of a program?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32827
    
    4
Shutdown hooks are really easy to use. If you search, you find lots of links, of which I am showing two: 1 2. I think no 1 is better.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Is there any way to close all existing file handlers when the java application ends?
 
Similar Threads
MS Access performance issue
Threading Question
deleteOnExit() - File Method
Log4j: How to retrieve RollingFileAppender's file name and path?
Static variables problem