Take the 2-minute tour ×


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

This part of code is used to save files with a given directory path to file system. Before saving i want to check whether the given save directory exists. However the existence check do not seem to work in the way that i wanted. Without removing the if clause the desired directories are not created.I came across to this interesting stack question while searching for my problem. Alternative to File.exists() in Java. As i understand java.io has this problem.

Is there a proper and safe way to check the existance of a directory or resource while doing file operations?

share|improve this question
Somewhat unrelated, but as long as you're being careful about it you should probably check isDirectory() too. –  Kevin Nov 3 '11 at 14:35

4 Answers 4

up vote 6 down vote accepted

Well, even if the check would be correct, you can never be sure that the directory still exists after the if condition has been evaluated. Another process or user can just create/remove it. So you have to check if the operation fails (possibly catching the appropriate exception) anyway.

So you shouldn't rely on checks and expect the worst case always. (Well, the checks may be useful for preventing you from doing something unnecessary, or for asking user for confirmation etc. But they don't guarantee anything.)

share|improve this answer
Thank you for the answer. The rest of the code initiates file save operation with input and output stream, thus needed to handle or throw IOException. But still i do not want to my code to try to call a directory creating method for an existing one. –  fga Nov 3 '11 at 14:42
@fga: well, if the directory suddenly appears/disappears, there is anyway something wrong going on. Your code should behave correctly and shouldn't throw if everything goes well -- but you have to process the exception for the case when something goes wrong. So it's okay to check if the directory exists, because it would work in the normal workflow. But as this check is not reliable, you cannot say you can't say the exception won't be thrown. –  Vlad Nov 3 '11 at 14:50
new File(dir).getCanonicalFile().isDirectory();  

Skeleton for your reference:-

File f = new File("....");
if (!f.exists()) {
    // The directory does not exist.
} else if (!f.isDirectory()) {
    // It is not a directory (i.e. it is a file).
share|improve this answer
Yet another instance of the answer with more votes being useful, rather than the accepted answer. –  Amala Jul 26 '13 at 20:58

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.

share|improve this answer

Just make the call to mkdirs. It will not overwrite existing directories so really, your check is unecessary (and in any case, unreliable).

share|improve this answer

Your Answer


