Friday, January 15, 2016

Create Directory / Directories in Java

You can use the mkdir and mkdirs method of the File class to create a directory or a layer of 
directories.

1. Create a directory

This is the sample code of creating a DATA directory in your current directory.

       import java.io.File;

       public class CreateADirectory {
             public static void main(String[] args) {
                   //Create a File class with the directory name
                   File dir = new File("Data");

                  //Create the directory here
                  boolean suc = dir.mkdir();

                  if (suc) {
                        System.out.println("The Data directory is successfully created.");
                  } else {
                          System.out.println("Failed to create the Data directory");
                  }
            }
       }

2. Create a layer of directories

Here is the sample code of creating a directory and all its parent directories that do not exist.

        import java.io.File;

        public class CreateDirectories {
               public static void main(String[] args) {
                     //Create a File class with the directory name
                     File dir = new File("Company\\Employee\\Data\\PersonalInfo");

                    //Create the directory here
                     boolean suc = dir.mkdirs();

                     if (suc) {
                           System.out.println("All directories are successfully created.");
                     } else {
                            System.out.println("Failed to create directories");
                     }
               }
        }

------------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment