当我使用下面的代码时,即使D盘中不存在\scrshot\vb,它也会创建\scrshot\vb\uv。
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();
}
}
如果我的理解是正确的,如果父文件夹不存在,mkdir() 将不会创建父文件夹。但这里它在D盘下创建\scrshot\vb\。这个thread供引用。
最佳答案
我假设您正在使用 Apache Commons FileUtil
。 documentation明确指出如果目标文件不存在,则将创建包含目标文件的目录:
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.
关于java - Mkdir 创建父目录(如果不存在),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61127026/