Home Tutorial Java Core Files Java file delete on exit

 
 

Share on Google+Share on Google+
Java file delete on exit
Posted on: April 14, 2006 at 12:00 AM
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();
	}
}
Advertisement

Related Tags for Java file delete on exit:


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.

Posted on: April 14, 2006

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com