Creating folder and sub folder where sub folder should be created one beside another in java

I’m facing an issue in the folder creation. Please find my requirement below

Each time you run the tests, a new folder is created with the time stamp.
Under the time stamp folder, another folder should get created. For example,
Under this sub folder new folders should get created one beside another and no duplicates are allowed.
Try - 1

public static File outputFile;

public static void screenshot_TimeStamp_Language_Folder(String language){

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
    outputFile = new File(timeStamp+"./L"+"_"+language);
    outputFile.mkdir();
    System.out.println(outputFile);

}

public static void screenshot_TestCaseFolder(String testCaseFolderName){

    String st = outputFile.getAbsolutePath();
    outputFile = new File(st+"./xyz_"+testCaseFolderName);
    outputFile.mkdir();
    System.out.println(outputFile);

}

public static void CaptureScreen(AppiumDriver driver, String imageFileName)
{

    File scrFile = driver.getScreenshotAs(OutputType.FILE);
    //String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());

    String path = outputFile.getAbsolutePath();
    System.out.println(path);

    File outputFile = new File(path + "/" + imageFileName +".jpg");
    try {
        FileUtils.copyFile(scrFile, outputFile);
    }
    catch (IOException ex) {
        System.out.println(Level.SEVERE + " Failed to save screen shot to " + outputFile);
    }
}

But I’m failing in the third step (i,e instead of creating sub folders one beside the another its creating folder inside the folder if you are calling screenshot_TestCaseFolder() method for more than one time in the same execution)

EX: public void test(){ screenshot_TestCaseFolder(); screenshot_TestCaseFolder(); screenshot_TestCaseFolder(); }

Please help me in resolving this issue

Thanks in advance

Hi,
I think you should:

  1. Store the timestamp in a variable

  2. Check the folder is existed or not. Please refer: http://stackoverflow.com/questions/15571496/how-to-check-if-a-folder-exists

  3. IF not, create a new one to store screenCapture

  4. IF YES, store the the old folder.

How do you think about that approach?

Hi Guys,

I found the solution

public static File outputFile;

public static void screenshot_TimeStamp_Language_Folder(String language){

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(Calendar.getInstance().getTime());
    outputFile = new File(timeStamp+"./L"+"_"+language);
    outputFile.mkdir();

}

//This is for first time folder creation
public static void screenshot_TestCaseFolder(String testCaseFolderName){

    String st = outputFile.getAbsolutePath();
    outputFile = new File(st+"./ANMM_"+testCaseFolderName);
    outputFile.mkdirs();

}

//This is for second time folder creation
public static void screenshot_TestCaseFolder1(String sample){

    String st = outputFile.getAbsolutePath();
    System.out.println("The first path is"+st);
    String str = outputFile.getParent();
    System.out.println("The second path is"+str);
    outputFile = new File(str+"/"+"/"+sample);
    //outputFile = new File(st+"./ANMM_" + "/" +testCaseFolderName);
    outputFile.mkdirs();

}