2016-09-19 45 views
1

我在創建文件夾時遇到問題。請在下面找到我的要求在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(); }

請幫我在提前解決這一問題

感謝

回答

0

我找到了解決辦法

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

    } 
相關問題