Java better way to delete file if exists

后端 未结 10 650
慢半拍i
慢半拍i 2020-12-24 10:21

We need to call file.exists() before file.delete() before we can delete a file E.g.

 File file = ...;
 if (file.exists()){
     fil         


        
10条回答
  •  清酒与你
    2020-12-24 10:58

    Use Apache Commons FileUtils.deleteDirectory() or FileUtils.forceDelete() to log exceptions in case of any failures,

    or FileUtils.deleteQuietly() if you're not concerned about exceptions thrown.

提交回复
热议问题