Java Create Directory - Java Tutorial

In the section of Java Tutorial you will learn how to
create directory using java program. This program also explains the process of
creating all non-existent ancestor directories automatically. We will use the class
File class to crate the directory.
Class File
The File class an abstract representation of file and directory pathnames. File
class is used to interact with the files system.
Here is the code for creating directory and all non-existing ancestor
directories:
import java.io.*;
class CreateDirectory
{
public static void main(String args[])
{
try{
String strDirectoy ="test";
String strManyDirectories="dir1/dir2/dir3";
// Create one directory
boolean success = (new File(strDirectoy)).mkdir();
if (success) {
System.out.println("Directory: " + strDirectoy + " created");
}
// Create multiple directories
success = (new File(strManyDirectories)).mkdirs();
if (success) {
System.out.println("Directories: " + strManyDirectories + " created");
}
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
|
Download the code

|
Current Comments
8 comments so far (post your own) View All Comments Latest 10 Comments:I'm a trainee s/w engineer.
I want to create an application in java. After completing the project I want to install it as a complete project i.e. java software .But I don't know how to create exe for installation.
Posted by subodh on Wednesday, 07.30.08 @ 19:18pm | #69999
simple but effective example.. very easy to follow
Posted by srikanth on Monday, 12.3.07 @ 17:44pm | #41185
Thank you very much,a really simple and practice example!
Posted by Manuela on Tuesday, 01.9.07 @ 23:43pm | #3072
Wht can I say abt roseindia.net, It is awesome. Excellent course material for beginners, It made me easy to brush-up JAVA, actually I forgot JAVA since I was very much into other application programming.
Now, I m very much confident abt JAVA, Thank U RoseIndia for ur valuable effort.
Posted by Rajee L on Tuesday, 12.19.06 @ 14:27pm | #1231
thank you very much. It's easy to understand.
Posted by arbujung on Thursday, 12.14.06 @ 10:07am | #1003
Thanks a lot it did help :)
Posted by SLizer3D on Saturday, 12.9.06 @ 13:56pm | #687
Its really useful and very easy to knew the basic concepts with out any illusion.Absolutely great feast to the beginners.
Posted by Balakrishnan.R on Wednesday, 12.6.06 @ 20:31pm | #534
It's really nice.Basic flow depicted here really providing track for the begineers to continue and brings confidence in them to proceed further.Keep it up.
Posted by Balakrishnan.R on Wednesday, 12.6.06 @ 20:06pm | #515