本文主要是介绍File 的mkdirs 创建文件夹,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
//File中mkdirs的实现public boolean mkdirs() {if (exists()) {return false;}if (mkdir()) {//【1】return true;}File canonFile = null;try {//【2】canonFile = getCanonicalFile();} catch (IOException e) {return false;}File parent = canonFile.getParentFile();return (parent != null && (parent.mkdirs() || parent.exists()) &&canonFile.mkdir());//【3】}
mkdirs()可以建立多级文件夹, mkdir()只会建立一级的文件夹, 如下:
new File("/tmp/one/two/three").mkdirs();
执行后, 会建立tmp/one/two/three四级目录
new File("/tmp/one/two/three").mkdir();
则不会建立任何目录, 因为找不到/tmp/one/two目录, 结果返回false
这篇关于File 的mkdirs 创建文件夹的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!