为什么要使用 FileUtils.forceMkdir?
Why use FileUtils.forceMkdir?
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 的实现是 "louder":如果要创建的目录已经存在,它会抛出异常,但它实际上是一个文件。看起来这个想法是包装 returns false
的 JRE 实现,而不是在目录创建不成功的情况下出现异常。
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 的实现是 "louder":如果要创建的目录已经存在,它会抛出异常,但它实际上是一个文件。看起来这个想法是包装 returns false
的 JRE 实现,而不是在目录创建不成功的情况下出现异常。