file.mkdirs 和mkdir的区别

2018-12-05  本文已影响5人  ae12

mkdirs:会一步到位创建目录文件(即使目录不存在);
mkdir:如果找不到上一级的目录,会创建失败
mkdirs() will create the specified directory path in its entirety where mkdir() will only create the bottom most directory, failing if it can't find the parent directory of the directory it is trying to create.

In other words mkdir() is like mkdir and mkdirs() is like mkdir -p.

For example, imagine we have an empty /tmp directory. The following code
代码块
new File("/tmp/one/two/three").mkdirs();
would create the following directories:

/tmp/one
/tmp/one/two
/tmp/one/two/three
Where this code:

new File("/tmp/one/two/three").mkdir();
would not create any directories - as it wouldn't find /tmp/one/two - and would return false.

上一篇 下一篇

猜你喜欢

热点阅读