创建文件夹:File 类中的方法 mkdir 与 mkdirs 的区别

mkdir与mkdirs相同之处

1.都是用来创建文件夹的.

2.要创建的文件夹存在或者成功都会返回Boolean值表示创建成功与失败,true表示存在或者创建成功,false表示创建失败.

mkdir与mkdirs不相同之处

mkdirs()可以建立多级文件夹
mkdir()只会建立一级的文件夹

1.利用mkdirs()方法创建多级文件夹.

new File("D:/test/img").mkdirs();

注意:执行后, 会在D盘路径下建立/create/firstLevel/twoLevel/threeLevel四级文件夹

点击查看 源码

/**
 * 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.
 *
 * @return  <code>true</code> if and only if the directory was created,
 *          along with all necessary parent directories; <code>false</code>
 *          otherwise
 *
 * @throws  SecurityException
 *          If a security manager exists and its <code>{@link
 *          java.lang.SecurityManager#checkRead(java.lang.String)}</code>
 *          method does not permit verification of the existence of the
 *          named directory and all necessary parent directories; or if
 *          the <code>{@link
 *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
 *          method does not permit the named directory and all necessary
 *          parent directories to be created
 */
public boolean mkdirs() {
    if (exists()) {
        return false;
    }
    if (mkdir()) {
        return true;
    }
    File canonFile = null;
    try {
        canonFile = getCanonicalFile();
    } catch (IOException e) {
        return false;
    }

    File parent = canonFile.getParentFile();
    return (parent != null && (parent.mkdirs() || parent.exists()) &&
            canonFile.mkdir());
}

2.利用mkdir()方法创建多级文件夹会失败返回false, mkdir()只会建立一级的文件

new File("E:/create/firstLevel/twoLevel/threeLevel").mkdir();

注:不会在E盘下建立任何文件夹, 因为找不到/create/firstLevel/twoLevel/threeLevel文件夹, 结果返回false.

点击查看 源码

/**
 * Creates the directory named by this abstract pathname.
 *
 * @return  <code>true</code> if and only if the directory was
 *          created; <code>false</code> otherwise
 *
 * @throws  SecurityException
 *          If a security manager exists and its <code>{@link
 *          java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
 *          method does not permit the named directory to be created
 */
public boolean mkdir() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkWrite(path);
    }
    if (isInvalid()) {
        return false;
    }
    return fs.createDirectory(this);
}

在springboot中应用:

reggie:
  path: D:\img\
上传文件 代码
@RestController
public class CommonController {

    @Value("${reggie.path}")
    private String basePath;

    @PostMapping("/upload")
    public String upload(MultipartFile file) {
        // file是一个临时文件,需要转存到指定的位置,否则本次请求完成后临时文件会被清理

        // 获取原始文件名
        String filename = file.getOriginalFilename();  // abc.png
        String suffix = filename.substring(filename.lastIndexOf("."));  // 截取文件后缀
        // 或使用 UUID 重新生成文件名,防止文件名称重复造成文件覆盖
        filename = UUID.randomUUID().toString() + suffix;

        // 创建一个目录对象
        File dir = new File(basePath);
        // 判断当前目录是否存在
        if (!dir.exists()) {
            // 只使用目录存在
            // dir.mkdir();
            // 目录不存在,就创建
            dir.mkdirs();
        }

        try {
            file.transferTo(new File(basePath + "1.png"));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return filename;
    }
}



posted @ 2023-03-24 15:36  笔兴洽谈室  阅读(255)  评论(0编辑  收藏  举报