java - 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.
解决方案
我假设您使用的是 Apache Commons FileUtil
。文档明确指出,如果包含目标文件的目录不存在,则将创建它:
此方法将指定源文件的内容复制到指定的目标文件。如果保存目标文件的目录不存在,则创建该目录。如果目标文件存在,则此方法将覆盖它。
推荐阅读
- javascript - 如何将 Markdown 解析为 AST,对其进行操作,然后将其写回 Markdown?
- c++ - 为许多文件.h和文件.cpp设置文件launch.json和tasks.json(文件.h的实现)
- r - 如何在 R 中订阅 GCP Pub/Sub 消息?
- python - 带有字典列表的 Python 游戏/每个问题有 2 个部分
- javascript - axios 不通过 http-proxy-middleware 向服务器发送 post 请求
- r - 具有多条线的 R 闪亮线图
- php - PHP:使用 GOTO 跳过 HTML 的呈现
- mysql - 如何将文件添加到数据库 my sql 和 symfony 5
- flutter - 在 SliverToBoxAdapter 中颤动 tabbarview
- android - 发送 Firebase 主题消息时是否存在预期的延迟带,并且当您拥有大量订阅者的主题时它会恶化吗?