ADVERTISEMENT
ADVERTISEMENT

Java program to create directory/folder in particular drive

In this Java program, we will learn how we can create a directory or a folder? To create a directory, we use File.mkdir() method in Java.
Submitted by IncludeHelp, on October 27, 2017

File.mkdir()

This is a method of "File" class in Java and used to create a directory at specified path, it return true if directory creates or returns false if directory does not create.

Here, first of all we are creating an object name dir of "File" class by specifying the "directory name with the path (drive name)" and then we are using dir.mkdir() method to create directory (Here, dir is an object of "File" class).

Program to create directory in Java

import java.io.*;

public class CreateDirectory {
  public static void main(String[] args) {
    //object of File class with file path and name
    File dir = new File("E:/IHelp");

    //method 'mkdir' to create directroy and result
    //is getting stored in 'isDirectoryCreated'
    //which will be either 'true' or 'false'
    boolean isDirectoryCreated = dir.mkdir();

    //displaying results
    if (isDirectoryCreated)
      System.out.println("Directory successfully created: " + dir);
    else
      System.out.println("Directory was not created successfully: " + dir);
  }
}

Output

Directory successfully created: E:\IHelp

Java File Handling Programs »





ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.