Google+

260. Creating Folders using mkdir( ) method







Pre-requisites -


mkdir( ) method is used to create the folders. In order to create it we have to create a File Class object by providing the Name of the Folder and use the mkdir( ) method withe File Class created object as shown in the below example -

Example -

File dir1 = new File("ABZ");  //Specify the Folder name here

dir1.mkdir( ):  //Creates the folder with the above specified name



Lets implement this on Eclipse IDE -

1. Launch Eclipse IDE, create a new Java Class 'CreateFoldersDemo.java' with main( ) method in the existing Java Project 'Project 50' as shown below -


 2. Create a File Class object say 'dir1'  by specifying the Folder Name say 'ABZ' as shown below (resolve any import errors) -


3. Create a directory using mkdir( ) method with the above created dir1 File Class object as shown below -



4. Check our Project Workspace to verify that a folder with 'ABZ' name is not available before running the Java Program as shown below -



5. Save & Run the Java Class 'CreateFoldersDemo.java' and observe that a new folder got created in our Project Workspace as shown below -


Hence we can create folders using mkdir( ) method.






Please comment below to feedback or ask questions.

Using getPath( ) and getAbsolutePath( ) methods will be explained in the next post.











No comments: