Home Tutorial Java Core Files Java file delete on exit

 
 

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

Related Tags for Java file delete on exit:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.