So, I want to create a directory in Java. In case of the directory already existing that function would do nothing and won't throw an error, but if it doesn't exist, it would create one. Does such a function exist in Java?
Asked
Active
Viewed 82 times
0
-
Might be mistaken, but it sounds like you're just looking for a try-catch where nothing happens on the catch? – beefoak Oct 31 '18 at 13:05
-
The accepted answer should have you covered. If not, drop a comment and someone can reopen the question. – Tim Biegeleisen Oct 31 '18 at 13:07
-
Hmm, you mean to have FileAlreadyExistsException and do nothing inside it. – AlexScalar Oct 31 '18 at 13:08
-
1Alternatively you could use [Files.createDirectories(Path dir, FileAttribute>... attrs)](https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createDirectories(java.nio.file.Path,%20java.nio.file.attribute.FileAttribute...)) which will not raise an exception if the dir already exists. It differs in that it would lead to the creation of the parent dirs were those not to exist. (actually its use is covered in the second most upvoted answer of the linked duplicate) – Aaron Oct 31 '18 at 13:11
-
I've found the solution. I can just use File.mkdirs. – AlexScalar Oct 31 '18 at 13:30