Java file delete and System.gc()

前端 未结 2 1636
一个人的身影
一个人的身影 2021-01-22 10:45

In my app I\'m writing a file to store some execution info and once the execution is done I want to delete the temp file. The issue is even after file close or flush by streams

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-22 11:26

    What you want to use is File.createTempFile() and call deleteOnExit() on the resulting file. It will manage cleaning up the temp file at the end of execution for you. You shouldn't be doing this manually, let the system do this for you, that is why they included this in the standard library.

    Also you should always call .close() to make sure that all the data is flushed to the file and the handle can be cleaned up correctly.

提交回复
热议问题