Create a directory in Java

Posted on 07-01-2013 12:25 by graham
This short tutorial will show you how to check if a directory exists in Java, and if it doesn't, create it.

Checking if a directory exists:
File dir = new File("directory-name");
if (!dir.exists() || !dir.isDirectory())
{
...
}

Creating a directory:
dir.mkdir();

... and the complete code:
File dir = new File("directory-name");
if (!dir.exists() || !dir.isDirectory())
{
dir.mkdir();
}

No big deal!
Comments

 

Add comment

Has this tutorial been helpful to you? Or do you see anything wrong? We appreciate your opinion!
Your comment:
Show formatting hints
HTML is disallowed, but in your text you can use the following markup
  • [code][/code] for a block of code
  • [tt][/tt] for inline code
  • [link]link href|link anchor[/link] for links
  • [b][/b] for bold text
Email:
+ Ask a question
If you have a technical question related to programming and computers, ask it here. Other users will help you solve it!
Unanswered questions
Share your knowledge by helping others solve their problems