A file is a handle to a real file (whether it exists or not). You create and then delete the file above, as you say, everything is still there.
When you come to PrintWriter later, it will create the file again when you use it - it doesn't matter that you deleted it before.
In fact, depending on your use case, it may be unconditional what you want - you can, for example, delete the old log file before re-creating and writing it again.
Finally, nothing in your code will have the right to garbage collection until your method exists, and even then the underyling file will continue to exist (unless you delete it) - and any garbage collector in this case will not affect base file. It will be deleted after the delete call and will exist again after you do PrintWriter with it.
Hope this helps!
source share