title: Shutdownhook in Java
body: <p>I want to write the system shutdown time to a txt file. I am using the shutdownhook thread.</p>
  
  <p>I have written the file writing mechanism in the run method of the thread. But it is not working.. This is my code..</p>
  
  <pre><code>public class JVMShutdownHookTest {
    public static void main(String[] args) {
      JVMShutdownHook jvmShutdownHook = new JVMShutdownHook();
      Runtime.getRuntime().addShutdownHook(jvmShutdownHook);
      System.out.println("JVM Shutdown Hook Registered.");
      System.out.println("Pre exit.");
  
  
    }
  
    private static class JVMShutdownHook extends Thread {
      public void run() {
        System.out.println("JVM Shutdown Hook: Thread initiated.");
  
          File file = new File("C:\\Users\\karthi\\Desktop\\Shutdown.txt");
  
  
          try {
              //FileWriter fw = new FileWriter(file, true);
              PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
              pw.println("Shutdown Time is ======= " + Calendar.getInstance().getTime());
              pw.close();
          } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
  
      }
    }
  }
  </code></pre>