Reputation: 1415
I need a directory to save files but I am not sure whether it exists.
So I need to check if it exists first, and create it if necessary.
File saveDir = new File("/tmp/appname/savedir/");
if(!saveDir.exists()){
saveDir.mkdirs();
}
As above, there is a question.
Method "saveDir.exists()" returns a boolean value which indicates if the file path exists.
Of course, I could write some redundant and ugly code to work.
Is there a way to write some elegant code to achieve this goal?
Upvotes: 5
Views: 12186
Reputation: 15572
file.exists() will return whether a directory OR a file exists. You can also use isDirectory() to check if the file path is a directory or not
if (file.isDirectory()){
//do somethign because this directory already exists
}
else if (file.isFile()){
//do somethign because this file already exists
}
else {
boolean created = file.mkdirs();
if (!created) {
throw new IOException("Cannot create directory " + file);
}
}
As to how elegant this is.... I would wrap it in a util method personally to hide some of this extra cruft java makes you write
Upvotes: -1
Reputation: 457
There is always
if(!file.exists() && !file.mkDirs()) { // handle the failed to create situation... }
Upvotes: 6
Reputation: 193
File dir = new File(dirPath);
if(!dir.exists())
if(!dir.mkdirs()) {//throw or handle exception here.}
You would still need to throw or Handle SecurityException that might be thrown during File.mkdirs()
Upvotes: 0
Reputation: 311052
There is no point in pre-testing. It just introduces a timing window during which the actual status can still change, so you can still be wrong. Just call mkdirs()
and test the result it returns. If false, it did nothing; if true, it did something. What exactly it did is really of no actual interest.
Upvotes: 3
Reputation: 40076
is your question about "redundant" check-directory-exist code, or you need to create a directory and all missing parent directory?
I believe both can be easily done by using FileUtils in Apache Commons IO:
FileUtils.forceMkDir(new File("/some/missing/parent/directory/foo"));
Upvotes: 8
Reputation: 30485
I would create a static method in a utility class which saves a file and automatically creates all necessary directories at the same time. The code inside the utility method may be a bit verbose, but this way, you can keep that ugly, verbose code out of your high-level application logic. And in future projects, you can always reuse the utility. After you split this code off into a utility method, it will also be easy to test in isolation.
I recommend you try to think in terms of building utilities and then using the utilities to build the actual application, rather than mixing detailed, low-level code in everywhere. The utilities you write may be useful on later projects, but that's not the primary goal: even if you only use a utility method on one project, and even if it is only called from one place in the higher-level code, it is still worth it if it helps make the higher-level code clearer. I don't remember which book helped me to get this concept; it may have been The Practice of Programming by Kernighan and Pike, or The Art of UNIX Programming by Raymond, or On Lisp by Graham -- or probably all three. (Of course, it's also important to know your language's standard library, so you don't start writing utilities which are already written for you.)
Upvotes: 2
Reputation: 7958
As @stephan mentioned in his answer
Elegance is more of a subjective thing
But since you asked for it
import java.io.File;
public class FileBuilder {
File file;
public FileBuilder(String path) {
file = new File(path);
}
public FileBuilder createIfDirDoesNotExists() {
if (!file.exists()) {
file.mkdirs();
}
return this;
}
public File getFile() {
return file;
}
}
then you can do
class test {
public test() {
File file = new FileBuilder("/tmp/appname/savedir/").createIfDirDoesNotExists().getFile();
}
}
Upvotes: 1
Reputation: 7388
Elegance is more a subjective thing, but if you want to check whether the directories are actually created, check also the return value of File.mkdirs()
. If it returns false
it depends on your application on how to handle it (e.g. throw an exception).
Upvotes: 3