banner



How To Create Folder In Java

Technical Questions and Answers

  • Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Effective Resume Writing
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

How to create a new directory by using File object in Java?


The class named File of the java.io package represents a file or directory (path names) in the system. This class provides various methods to perform various operations on files/directories.

Creating a new directory

The mkdir() method of this class creates a directory with the path represented by the current object.

Therefore, to create a directory −

  • Instantiate the File class by passing the path of the directory you need to create, as a parameter (String).
  • Invoke the mkdir() method using the above created file object.

Example

Following Java example reads the path and name of the directory to be created, from the user, and creates it.

import java.io.File; import java.util.Scanner; public class CreateDirectory {    public static void main(String args[]) {       System.out.println("Enter the path to create a directory: ");       Scanner sc = new Scanner(System.in);       String path = sc.next();       System.out.println("Enter the name of the desired a directory: ");       path = path+sc.next();       //Creating a File object       File file = new File(path);       //Creating the directory       boolean bool = file.mkdir();       if(bool){          System.out.println("Directory created successfully");       }else{          System.out.println("Sorry couldn't create specified directory");       }    } }

Output

Enter the path to create a directory: D:\ Enter the name of the desired a directory: sample_directory Directory created successfully

If you verify, you can observe see the created directory as −

But, if you specify a path in a drive that doesn't exist, this method will not create the required directory.

For example, if the D drive of my (windows) system is empty and if I specify the path of the directory to be created as −

D:\test\myDirectories\sample_directory

Where the test and myDirectories folders doesn't exist, the mkdir()method will not create it.

Creating directory hierarchy

To create a hierarchy of new directories you can using the method mkdirs() of the same class. This method creates the directory with the path represented by the current object, including non-existing parent directories.

Example

import java.io.File; import java.util.Scanner; public class CreateDirectory {    public static void main(String args[]) {       System.out.println("Enter the path to create a directory: ");       Scanner sc = new Scanner(System.in);       String path = sc.next();       System.out.println("Enter the name of the desired a directory: ");       path = path+sc.next();       //Creating a File object       File file = new File(path);       //Creating the directory       boolean bool = file.mkdirs();       if(bool){          System.out.println("Directory created successfully");       }else{          System.out.println("Sorry couldnt create specified directory");       }    } }

Output

Enter the path to create a directory: D:\test\myDirectories\ Enter the name of the desired a directory: sample_directory Directory created successfully

If you verify you can observe see the created directory as −

raja

Published on 01-Aug-2019 13:54:30

  • Related Questions & Answers
  • Is it possible to change directory by using File object in Java?
  • How to create a directory using Java?
  • How to create a new directory in Linux using the terminal?
  • Create a new empty file in Java
  • How to create a Directory recursively using Java?
  • How to create a directory hierarchy using Java?
  • Create temporary file in specified directory in Java
  • How to create a directory in project folder using Java?
  • How to create a directory using Python?
  • How to create a Directory using C#?
  • How to search a file in a directory in java
  • How to copy files to a new directory using Python?
  • Moving a file from one directory to another using Java
  • Create a directory in Java
  • How to create a JSON Object using Object Model in Java?

How To Create Folder In Java

Source: https://www.tutorialspoint.com/how-to-create-a-new-directory-by-using-file-object-in-java

Posted by: linseymarban.blogspot.com

0 Response to "How To Create Folder In Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel