0

When I use below code, Its creating \scrshot\vb\uv even though \scrshot\vb not exist in D drive.

takeScreenshot("D:\\scrshot\\vb\\uv", "s.png");

    public void takeScreenshot(String fileDir,String fileName)
    {


        File directory=new File(fileDir);
        if(!directory.exists())
            directory.mkdir();
        File file=((TakesScreenshot)ddr).getScreenshotAs(OutputType.FILE);
        try {
            FileUtils.copyFile(file,new File(directory.getAbsolutePath()+File.separator+fileName));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }



    }

If my understanding is correct mkdir() would not create parent folder if it does not exist. but here its creating \scrshot\vb\ under D drive. this thread for reference.

J_Coder
  • 707
  • 5
  • 14
  • 32

2 Answers2

3

I assume you are using Apache Commons FileUtil. The documentation states clearly that the directory containing the destination File will be created if it doesn't exist:

This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.

Axel
  • 13,939
  • 5
  • 50
  • 79
0

mkdir() never create parent directory

but in your code

FileUtils.copyFile(file,new File(directory.getAbsolutePath()+File.separator+fileName));

This line is responsible to create a directory.