Share on Google+Share on Google+

Java file delete on exit

Advertisement
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();
	}
}

Advertisements

Posted on: April 14, 2006 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.