Tuesday 26 June 2012

Create Directory in Java

We can use java.io.File class for creating directories in Java. A File class object is an abstract representation of file and directory pathnames.

new File("pathname");

Creates a new File instance by converting the given pathname string into an abstract pathname. If the given string is the empty string, then the result is the empty abstract pathname.

The java.io.File class provides many methods for getting information about a file system. Please visit Load a file in Java to learn more about it.

We can use java.io.File class mkdir(), mkdirs() methods for creating directories.

public boolean mkdir()


Creates the directory named by this abstract pathname.


The mkdir() returns true if and only if the directory was created; false otherwise.Throws SecurityException - If a security manager exists and it does not permit the named directory to be created.

File f = new File("/home/radhakrishnan/test/test1");
boolean success=f.mkdir();



In the above code, f.mkdir() creates a directory named test1 and returns true.The directory creatiion may fail because of the following reasons.

  • The directory test1 already exists.
  • Parent directories(here directory 'test') does't exist.
  • If the user does not have permission to create directory.
  • SecurityException occured. If the Security Manager does not permit the named directory to be created.


    The complete code is given below.

    import java.io.File;
    
    public class CreateDirectory {
    
        public static void main(String args[]) {
    
            File f = new File("/home/radhakrishnan/test");
            boolean success = f.mkdir();
            if (success) {
                System.out.println("Directory test created.");
            } else {
                System.out.println("Failed to create test directory.");
            }
        }
    
    }
    

    public boolean mkdirs()


    If you want to create the directory named by this abstract pathname, including any necessary but nonexistent parent directories, use File class mkdirs() method.This method returns true if and only if the directory was created, along with all necessary parent directories; false otherwise.

    File f = new File("/home/radhakrishnan/test/test1/test2/test3");
    boolean success=f.mkdirs();


    In the above code f.mkdirs() creates the directory test3 along with all necessary parent directories(creates directories test,test1 and test2 if it does't already exist) and returns true.

    The directory creatiion may fail because of the following reasons.

    • The directory test3 already exists.
    •  If the user does not have permission to create directory.
    • SecurityException occured. If the Security Manager does not permit the named directory and all necessary parent directories to be created. 

    The complete code is given below

    import java.io.File;
    
    public class CreateDirectories {
    
        public static void main(String args[]) {
    
            File f = new File("/home/radhakrishnan/test/test1/test2/test3");
            boolean success = f.mkdirs();
            if (success) {
                System.out.println("Directories created.");
            } else {
                System.out.println("Failed to create directories.");
            }
        }
    
    }
    

      No comments:

      Post a Comment