Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use of deleteOnExit() method in java

Tags:

what is the difference between delete() and deleteOnExit() methods in java.io.File class in Java?

like image 256
Vishal Chugh Avatar asked Sep 25 '13 18:09

Vishal Chugh


People also ask

How do I delete a file in a directory in Java?

To delete a file in Java, we can use the delete() method from Files class. We can also use the delete() method on an object which is an instance of the File class.

How do I delete a file in Java?

In Java, we can delete a file by using the File. delete() method of File class. The delete() method deletes the file or directory denoted by the abstract pathname. If the pathname is a directory, that directory must be empty to delete.


1 Answers

delete() will delete the file at once, while deleteOnExit() will not delete the file, when you call it. Instead the file is deleted, when the program ends or more precise the virtual machine terminates.

In case the virtual machines terminates not regulary, deleteOnExit() has no effect.

like image 143
Barny Avatar answered Oct 04 '22 17:10

Barny