首页 > 解决方案 > Mkdir Creates Parent Directory If not exist

问题描述

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.

标签: java

解决方案


我假设您使用的是 Apache Commons FileUtil文档明确指出,如果包含目标文件的目录不存在,则将创建它:

此方法将指定源文件的内容复制到指定的目标文件。如果保存目标文件的目录不存在,则创建该目录。如果目标文件存在,则此方法将覆盖它。


推荐阅读