6

Code:

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

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?

Community
  • 1
  • 1
fgakk
  • 1,289
  • 1
  • 15
  • 26
  • Somewhat unrelated, but as long as you're being careful about it you should probably check `isDirectory()` too. – Kevin Nov 03 '11 at 14:35

4 Answers4

28
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).
    ... 
}
Siva Charan
  • 17,940
  • 9
  • 60
  • 95
  • 4
    Yet another instance of the answer with more votes being useful, rather than the accepted answer. – Amala Jul 26 '13 at 20:58
6

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.)

Vlad
  • 35,022
  • 6
  • 77
  • 199
  • 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. – fgakk Nov 03 '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 03 '11 at 14:50
1

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

Perception
  • 79,279
  • 19
  • 185
  • 195
0

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.

Shivan Dragon
  • 15,004
  • 9
  • 62
  • 103