Thursday, March 22, 2012

How to Create Directory in Java,Directory Creation in Java

0 comments

Java Create Directory - Java Tutorial


In this Tutorial you will learn how to create directory and check whether that directory is exist.If the directory is exist then it will not create otherwise,


import java.io.File;

/**
 *
 * @author JSupport http://javasrilankansupport.blogspot.com
 */
public class CreateDirectory {

    public static void main(String[] args) {

        String directory = "JAVADIR";
        String directory_tree = "JSUPPORT/JAVA";
        boolean dir_exist = false;

        try {
            // Check whether the derectory is exist
            dir_exist = new File(directory).isDirectory();

            if (dir_exist) {
                System.out.println("directory already created.....");
            } else {
                // creat directory
                new File(directory).mkdir();
            }

            // Check whether the derectory is exist
            dir_exist = new File(directory_tree).isDirectory();

            if (dir_exist) {
                System.out.println("directories already created.....");
            } else {
                // create directories
                new File(directory_tree).mkdirs();
            }

        } catch (Exception e) {
            System.out.println("Error : "+e.getMessage());
        }

    }
}

0 comments:

Post a Comment