Java better way to delete file if exists

后端 未结 10 642
慢半拍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:50

      File xx = new File("filename.txt");
        if (xx.exists()) {
           System.gc();//Added this part
           Thread.sleep(2000);////This part gives the Bufferedreaders and the InputStreams time to close Completely
           xx.delete();     
        }
    

提交回复
热议问题