Java file delete on exit


 

Java file delete on exit

This section demonstrates you the use of the method deleteOnExit().

This section demonstrates you the use of the method deleteOnExit().

Java file delete on exit

This section demonstrates you the use of the method deleteOnExit().

Description of code:

While creating an application, sometimes you require to hold the data in a temporary file. The java.io.* package provides the method createTempFile() to create a temporary  file and you can delete it using the method deleteOnExit(). This method delete the file when the program terminates.

createTempFile()- This method of File class create temporary  file.

deleteOnExit()- This method of File class requests that the file or directory be deleted when the virtual machine terminates.

Here is the code:

import java.io.*;

public class FileDeleteOnExit {
	public static void main(String[] argv) throws Exception {
		File temp = File.createTempFile("file", ".tmp");
		temp.deleteOnExit();
	}
}

Ads