File mkdir() method in Java with examples
The mkdir() method is a part of File class. The mkdir() function is used to create a new directory denoted by the abstract pathname. The function returns true if directory is created else returns false.
Function Signature:
public boolean mkdir()
Syntax:
file.mkdir()
Parameters: This method do not accepts any parameter.
Return Value: The function returns boolean data type. The function returns true if directory is created else returns false.
Exception: This method throws SecurityException if the method does not allow directory to be created
Below programs will illustrate the use of mkdirs() function:
Example 1: Try to create a new directory named program in “f:” drive.
import java.io.*;
public class GFG {
public static void main(String args[])
{
File f = new File( "F:\\program" );
if (f.mkdir()) {
System.out.println( "Directory is created" );
}
else {
System.out.println( "Directory cannot be created" );
}
}
}
|
Output:
Directory is created
Example 2: Try to create a new directory named program1 in “f:\program” directory, but program directory is not created .we will test whether the function mkdir() can create the parent directories of the abstract path name if the directories are not present.
import java.io.*;
public class GFG {
public static void main(String args[])
{
File f = new File( "F:\\program\\program1" );
if (f.mkdir()) {
System.out.println( "Directory is created" );
}
else {
System.out.println( "Directory cannot be created" );
}
}
}
|
Output:
Directory cannot be created
The programs might not run in an online IDE. please use an offline IDE and set the path of the file
Last Updated :
28 Jan, 2019
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...