Delete file or directory when virtual machine terminates


/*
  Delete file or directory when virtual machine terminates
  This Java example shows how to delete a particular file or directory from filesystem
  when Java Virtual Machine terminates using deleteOnExit method of Java File class.
*/

import java.io.*;

public class DeleteFileWhenVMTerminates {

 
public static void main(String[] args) {
   
   
//create File object
   
File file = new File("C://FileIO//DeleteDemo.txt");
   
   
/*
     * To delete a particular file or directory when Java VM exits, use
     * void deleteOnExit() method.
     *
     * This method request to delete a specified file or directory to be deleted
     * when virtual machine terminates normally.
     * 
     */
    
    
file.deleteOnExit();
    
    
/*
      * Please note that, once deletion has been requested, it is not possible to
      * cancel that operation.
      */
 
}
}