Karunagara
Karunagara

Reputation: 389

How to create a new folder under given path using Java?

I have a scenario like, have to create a new folder with the time stamp in a particular location if that folder does not exist for the first time. And then I have to write the files inside that newly created folder.

For creating new folder under the given location, I have written the following code which is not creating the folder and it returns FALSE.

public static void writeRequestAndResponse()
{
    try
    {
        DateFormat format = new SimpleDateFormat("yyyy_MM_dd_HH:mm:ss");
        Date date = new Date();
        String currentDateTime = format.format(date);

        String folderPath = "D:\\working\\POC\\Output\\LastRunOn_" + currentDateTime;
        System.out.println(folderPath);

        File file = new File(folderPath);

        if (!file.exists())
        {
            boolean isDirCreated = file.mkdir();
            System.out.println(isDirCreated);
        }
    }
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
}

Existing Path : D:\working\POC\Output\
My Current Java Version: 1.6

Upvotes: 0

Views: 8021

Answers (1)

Abhi
Abhi

Reputation: 1005

Working code Example : Following your approach

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FolderDemo {

    public static void writeRequestAndResponse() {

        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy_MM_dd_HH.mm.ss");

        String currentDateTime = format.format(date);

        String folderPath = "F:\\working\\POC\\Output\\" + "LastRunOn_"
                + currentDateTime;

        File theDir = new File(folderPath);

        // if the directory does not exist, create it
        if (!theDir.exists()) {
            System.out.println("creating directory: " + theDir.getName());
            boolean result = false;

            try {

                theDir.mkdirs();
                result = true;
            } catch (SecurityException se) {
                // handle it
                System.out.println(se.getMessage());
            }
            if (result) {
                System.out.println("Folder created");
            }
        } else if (theDir.exists()) {

            System.out.println("Folder exist");
        }

    }

    public static void main(String[] args) {
        writeRequestAndResponse();
    }

}

Few things to remember :

  1. File or folder names cannot contain any of the following characters:\, /, :,

*, ?, "", <, >, |.

That is why your Time format "yyyy_MM_dd_HH:mm:ss" was not added to the folder name.

So I replaced " : " with " . "

How to create directory in Java :

To create a directory in Java, uses the following code:

  1. Standard Java IO package – java.io.File

1.1 Create a single directory.

new File("C:\Directory1").mkdir();

1.2 Create a directory named “Directory2 and all its sub-directories “Sub2” and “Sub-Sub2” together.

new File("C:\Directory2\Sub2\Sub-Sub2").mkdirs()

Both method mkdir() and mkdirs() are returning a boolean value to indicate the operation status : true if succeed, false otherwise.

Upvotes: 1

Related Questions