0

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

4
  • 1
    For recent java versions (IIRC 1.7 onwards), you could use Files.mkdirs() which will save you the hassle. Commented Feb 23, 2018 at 8:12
  • 3
    Windows does not allow you to have colons (:) in the filename. Take a valid file name that you can create on the command line, then produce that from Java. Commented Feb 23, 2018 at 8:12
  • @Norwæ The parent directory already exists ( D:\working\POC\Output\ ) so the issue isn't mkdir versus mkdirs. The problem is that the file name is not valid because it has a colon in it. Commented Feb 23, 2018 at 8:13
  • Hi Erwin Bolwidt, As per the suggestion, I removed the (:) and executed the code and its working...Thanks Commented Feb 23, 2018 at 8:15

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.