I want all text files (Notepad) created during run time to be deleted every time I close the program.
For example, while my program was running, files (ex. foods.txt, numbers.txt, and fruits.txt) were created, and I want those files to be deleted when I close or stop the program.
dont know if its possible to make
CodePudding user response:
This definitely is possible. You can do it in several ways:
While your program writes files, also add them to a list of files created by your application. On shutdown, iterate over this list and delete the files.
Or create a temp directory and all files inside of that. In the end, just delete that directory.
If you do not have a separate directory but at least can locate the file, remember the start time of the application. Upon closure traverse the filesystem to find files created after startup and delete those.
All in all you need to make up your mind how to detect the files to be deleted. I'd go for the temp directory. How to create a temporary directory/folder in Java?