创建文件夹和子文件夹,其中应该在java中创建子文件夹

时间:2016-09-19 14:02:02

标签: selenium-webdriver appium-ios

我在文件夹创建中遇到问题。请在下面找到我的要求

  1. 每次运行测试时,都会创建一个带有时间戳的新文件夹。
  2. 在时间戳文件夹下,应创建另一个文件夹。例如,
  3. 在此子文件夹下,新文件夹应相互创建,不允许重复。
  4. 尝试 - 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);
            }
        }
    

    但是我在第三步失败了(如果你在同一个执行中多次调用screenshot_TestCaseFolder()方法,而不是在另一个文件夹中创建子文件夹而不是创建子文件夹)

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

    请帮我解决这个问题

    提前致谢

1 个答案:

答案 0 :(得分: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();

    }