gpt4 book ai didi

java - 为什么使用 FileUtils.forceMkdir?

转载 作者:行者123 更新时间:2023-12-02 09:24:13 24 4
gpt4 key购买 nike

Apache commons-io 中的 FileUtils.forceMkdir 代替 JRE 中内置的 File.mkdirs 有何用途?

forceMkdir 的文档是

Makes a directory, including any necessary but nonexistent parent directories. If a file already exists with specified name but it is not a directory then an IOException is thrown. If the directory cannot be created (or does not already exist) then an IOException is thrown.

虽然 mkdirs 的文档是

Creates the directory named by this abstract pathname, including any necessary but nonexistent parent directories. Note that if this operation fails it may have succeeded in creating some of the necessary parent directories.

唯一的区别是抛出异常的类型吗?

最佳答案

看起来主要区别在于 Apache commons 的实现“更大声”:如果要创建的目录已经存在,但它实际上是一个文件,它将抛出异常。看起来这个想法是包装 JRE 实现,在目录创建失败的情况下返回 false 而不是异常。

关于java - 为什么使用 FileUtils.forceMkdir?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58453384/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com