

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Delete file or directory on termination in Java
A file or directory can be deleted on termination of the program i.e. after the virtual machine terminates using the method java.io.File.deleteOnExit(). This method requires no parameters and it does not return a value.
A program that demonstrates this is given as follows −
Example
import java.io.File; public class Demo { public static void main(String[] args) { try { File file = new File("demo1.txt"); file.createNewFile(); System.out.println("File: " + file); file.deleteOnExit(); } catch(Exception e) { e.printStackTrace(); } } }
The output of the above program is as follows −
Output
File: demo1.txt
Now let us understand the above program.
The file is deleted when the program ends i.e. after the virtual machine terminates using the method java.io.File.deleteOnExit(). A code snippet that demonstrates this is given as follows −
try { File file = new File("demo1.txt"); file.createNewFile(); System.out.println("File: " + file); file.deleteOnExit(); } catch(Exception e) { e.printStackTrace(); }
- Related Questions & Answers
- Delete the file or directory in Java
- Java Program to delete file or directory
- Rename file or directory in Java
- Java Program to delete a file or directory when the program ends
- Check for file or directory in Java
- Determine if file or directory exists in Java
- Mark file or directory Read Only in Java
- Delete a directory in Java
- Java Program to rename a file or directory
- Determine if File or Directory is hidden in Java
- Java Program to get name of specified file or directory
- Specify a path for a file or a directory in Java
- Get the absolute path for the directory or file in Java
- Java Program to get the name of the parent directory of the file or directory
- Java Program to check if a file or directory is readable
Advertisements