java - Mkdir 创建父目录(如果不存在)

标签 java

当我使用下面的代码时,即使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 FileUtildocumentation明确指出如果目标文件不存在,则将创建包含目标文件的目录:

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/

相关文章:

java - 调用远程方法时出错

java - 父类(super class)中没有可用的构造函数(Java)

java - NFC intent-filter Android - 匹配 application/vnd.wfa.wsc 记录

java - 多个 Web 服务的 stub 策略?

java - 如何检测 Netbeans 中的 Java 应用程序锁定?

java Swing : single click event to work as double click

java - 为什么有人会在 java 中使用 Collections.emptyList?

java - 为什么我的扫描仪类无法读取 3 变量

java - Action 和名称

java - JSONArray 数据不会显示在 Fragment 上