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
1.7onwards), you could useFiles.mkdirs()which will save you the hassle.:) in the filename. Take a valid file name that you can create on the command line, then produce that from Java.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.