Java better way to delete file if exists

后端 未结 10 665
慢半拍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 11:06

    There's also the Java 7 solution, using the new(ish) Path abstraction:

    Path fileToDeletePath = Paths.get("fileToDelete_jdk7.txt");
    Files.delete(fileToDeletePath);
    

    Hope this helps.

提交回复
热议问题