Leon Anavi
IT Tips && Tricks

Java

Created: 08.09.2009 00:31 Last Modified: 08.09.2009 11:25 Views: 3639
Keywords: File, mkdir, mkdirs

How to Create a Directory

Create Single or Multiple Directories

Class File is an abstract representation of files and directories which is a part of the java.io package. Methods mkdir and/or mkdirs are used for creation of a single or multiple directories. Both methods return boolean values - true on success and each of them throws a SecurityException on error.

Example

A console application is provided as a simple example how to create a single directory.
package dircreator;

import java.io.File;
/**
 *
 * @author Leon Anavi
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        if (1 != args.length)
        {
            String sUsage = "Please enter directory name as an argument.\n";
            System.out.println(sUsage);
            return;
        }
        try
        {
            File Dir = new File(args[0]);
            if ( true == Dir.mkdir() )
            {
                System.out.println("Directory has been created.");
            }
            else
            {
                System.out.println("Unable to create file.");
            }

        }
        catch(SecurityException Ex)
        {
            System.out.println( Ex.getMessage() );
        }
        catch(java.lang.ArrayIndexOutOfBoundsException Ex)
        {
            System.out.println( Ex.getMessage() );
        }
    }

}

Class Reference

class File


  Home | About | Contact | Disclaimer | Sitemap 2009-2016 Leon Anavi. All rights reserved.