Is there a proper way to check for file/directory existence in Java?

后端 未结 4 1852
不思量自难忘°
不思量自难忘° 2021-01-12 23:15

Code:

String dir = //Path to the  directory
File saveDir = new File(dir);
//Here comes the existence check
if(!saveDir.exists())
  saveDir.         


        
4条回答
  •  一整个雨季
    2021-01-12 23:57

    FileChannel.lock() does just what you want as long as it's not another thread in the JVM that's deleting the directory while you're using in. This thing demandes a OS lock on a file/folder on behalf of the JVM process, so while other processes will not be able to access that directory, threads in the JVM can.

提交回复
热议问题