Java.io.File.mkdir() Method

Advertisements


Description

The java.io.File.mkdir() creates the directory named by this abstract pathname.

Declaration

Following is the declaration for java.io.File.mkdir() method:

public boolean mkdir()

Parameters

  • NA

Return Value

The method returns true if the directory is created, else the method returns false.

Exception

  • SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method denies access to create the named directory.

Example

The following example shows the usage of java.io.File.mkdir() method.

package com.tutorialspoint;

import java.io.File;

public class FileDemo {
   public static void main(String[] args) {
      
      File f = null;
      boolean bool = false;
      
      try{      
         // returns pathnames for files and directory
         f = new File("C:/Texts");
         
         // create
         bool = f.mkdir();
         
         // print
         System.out.print("Directory created? "+bool);
         
      }catch(Exception e){
         // if any error occurs
         e.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result:

Directory created? true



Advertisements
Advertisements