英文:
Mkdir Creates Parent Directory If not exist
问题
以下是翻译好的部分:
当我使用下面的代码时,即使在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\。有关参考,请查看此线程。
英文:
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.
答案1
得分: 3
我假设您正在使用Apache Commons的FileUtil
。文档 清楚地说明,如果目标文件的目录不存在,将会创建它:
> 此方法将指定源文件的内容复制到指定的目标文件中。**如果目标文件的目录不存在,将会创建它。**如果目标文件已存在,那么此方法将覆盖它。
英文:
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.
答案2
得分: 0
mkdir()永远不会创建父目录
但在你的代码中
FileUtils.copyFile(file, new File(directory.getAbsolutePath() + File.separator + fileName));
这一行负责创建一个目录。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论