Java文件类boolean mkdirs()方法(带示例)

文件类布尔型mkdirs() (File Class boolean mkdirs())

  • This method is available in package java.io.File.mkdirs().

    软件包java.io.File.mkdirs()中提供了此方法。

  • This method is used to create a directory with all needed or necessary parent directories using mkdirs() method and this method is accessible with file object.

    该方法用于使用mkdirs()方法创建包含所有必需或必要的父目录的目录,并且该方法可通过文件对象访问。

  • If the directory already exists so we are not allowed to create a directory of the same name.

    如果目录已经存在,则不允许我们创建同名目录。

  • The return type of this method is Boolean i.e. it returns true or false if return true means directory is successfully created with including all necessary parent directories. Else return false.

    此方法的返回类型为Boolean,即如果返回true表示包含所有必需的父目录的目录已成功创建,则返回true或false。 否则返回false。

Syntax:

句法:

    boolean mkdirs(){
    }

Parameter(s):

参数:

We don't pass any object as a parameter in the method of the File.

我们不会在File方法中将任何对象作为参数传递。

Return value:

返回值:

The return type of this method is int, it returns true or false if true then the directory is successfully created with all parent directories else return false directory already exists or an exception occurs.

此方法的返回类型为int ,如果返回true,则返回true或false,然后使用所有父目录成功创建目录,否则返回false目录已存在或发生异常。

Java程序演示mkdirs()方法的示例 (Java program to demonstrate example of mkdirs() method)

// import the File class because we will use File class methods
import java.io.File;

// import the Exception class because it may raise 
// an exception when working with files
import java.lang.Exception;

public class CreateDirectoryUsingMkdirsMethod {
    public static void main(String[] args) {
        try {
            // Specify the path of file and we use double slashes to 
            // escape '\' character sequence for windows otherwise 
            // it will be considerable as url.
            File dir1 = new File("E:\\Java");
            File dir2 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles");

            // By using mkdirs()create a new directory named 
            // C:\\Users\\Articles including all parent directories because 
            // directory is not exists(i.e it returns true) .
            if (dir1.mkdirs())
                System.out.println("Directory created Successfully " + dir1.getName());
            else
                System.out.println("Directory already exists " + dir1.getName());

            // By using mkdirs() is not create a new directory named 
            // " C:\\Users\\computer clinic\\OneDrive\\Articles " 
            // because the name of this directory is already exists 
            // (i.e. it returns false) so we will get a message 
            // Directory already exists .
            if (dir2.mkdirs())
                System.out.println("Directory created Successfully " + dir2.getName());
            else
                System.out.println("Directory already exists " + dir2.getName());

        } catch (Exception e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
        }
    }
}

Output

输出量

E:\Programs>javac CreateDirectoryUsingMkdirsMethod.java

E:\Programs>java CreateDirectoryUsingMkdirsMethod
Directory created Successfully Java
Directory already exists Articles


翻译自: https://www.includehelp.com/java/file-class-boolean-mkdirs-method-with-example.aspx

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值