Tuesday, 28 July 2015

How to create Directory and Sub Directories in java?


For example I want to create software project developer structure directory.

     Project Manager


                Team Leaders

                        Senior software Engineer
                              Software Engineer.

 To create directory in java using below code.

1.       For create only one directory. For example I want to create only project manager directory.

new File(“D:\\Project Manager”).mkdir();

2.       For create sub directories .for example I want to create project Manager under Team Leads….etc.

new File(“D:\\Project Manager\\Team Leads\\Senior Software Engineers\\Software Engineers);

For Example first i have to take one class name called as a  CreateDirectory.java

CreateDirectory.java

import java.io.File;

public class CreateDirectory {
       public static void main(String[] args)
    {  
    File file = new File("D:\\Project Manager");
    if (!file.exists()) {
        if (file.mkdir()) {
            System.out.println("directory created successfully");
        } else {
            System.out.println("directory is not created");
        }
    }

    File files = new File("D:\\Project Manager\\Team Leads\\Senior Software          Engineers\\Software Engineers");
    if (!files.exists()) {
        if (files.mkdirs()) {
            System.out.println("sub directories created successfully");
        } else {
            System.out.println("failed to create sub directories");
        }
      }

     }

}

No comments:

Post a Comment