Here is a pretty good guide to signal handling in Java. It covers shutdown and termination cases as well, including Ctrl-C.
The specific call you want is in the Runtime, addShutdownHook.
There are still specific cases that you cannot handle... there is usually some way for the OS to just outright kill the application without giving it a chance to save itself. You can't cover every case through this mechanism. To quote the JavaDoc
In rare circumstances the virtual machine may abort, that is, stop running without shutting down cleanly. This occurs when the virtual machine is terminated externally, for example with the SIGKILL signal on Unix or the TerminateProcess call on Microsoft Windows. The virtual machine may also abort if a native method goes awry by, for example, corrupting internal data structures or attempting to access nonexistent memory. If the virtual machine aborts then no guarantee can be made about whether or not any shutdown hooks will be run.