TechieDrill Your World Of Technical Tutorials

Creating Folders using simple Java

11.22.2009 · Posted in Java
import java.io.*;
class FileEx
{
public static void main(String [] s) throws IOException
{
// File f = new File(”xyz.txt”);
// f.delete();
File f1 = new File(s[0]);
f1.mkdir();
File f2 = new File (f1,s[1]);
f2.mkdir();
File f3 = new File (f2,s[2]);
f3.mkdir();
if(f3.mkdir())
{
System.out.println(”Folder Created….”);
File ff = new File(f3,”akshatha.txt”);
ff.createNewFile();
}
else
{
System.out.println(”folder already exists…”);
}
}
}

It is quite easy to create folders using Java File concepts. Using java.io.file package we can create folders using mkdir(make directory) method. This can also handle folder already exists scenarios.

At the same time of creating folders it is also possible to create files of any extensions.

Try out the below sample code and explore things…

import java.io.*;

class FileEx

{

public static void main(String [] s) throws IOException

{

// File f = new File(”xyz.txt”);

// f.delete();


File f1 = new File(s[0]);

f1.mkdir();


File f2 = new File (f1,s[1]);

f2.mkdir();

File f3 = new File (f2,s[2]);

f3.mkdir();


if(f3.mkdir())

{

System.out.println(”Folder Created….”);


File ff = new File(f3,”sample.txt”);

ff.createNewFile();

}

else

{

System.out.println(”folder already exists…”);

}

}

}

Leave a Reply

You must be logged in to post a comment.