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