在java中创建文件夹和子文件夹应该在其中一个旁边创建一个文件夹

问题描述:

我在创建文件夹时遇到问题。请在下面找到我的要求在java中创建文件夹和子文件夹应该在其中一个旁边创建一个文件夹

  1. 每次运行测试时,都会创建一个带有时间戳的新文件夹。
  2. 在时间戳文件夹下,应创建另一个文件夹。例如,
  3. 在此子文件夹下,应该创建一个新的文件夹,并且不允许重复。

尝试 - 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); 
     } 
    } 

但我在第三步我失败(I,E,而不是创建子文件之一,它的另一个创建文件夹旁边的文件夹内,如果你在呼唤screenshot_TestCaseFolder( )方法在同一执行中多次)

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

请帮我在提前解决这一问题

感谢

我找到了解决办法

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(); 

    }