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

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

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

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

  • This method is used to create a new directory by using mkdir() method and this method is accessible with the File object.

    该方法用于通过使用mkdir()方法创建新目录,并且该方法可通过File对象访问。

  • If a directory already exists i.e the name of the created directory already exists that means 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 true that means directory is successfully created and returns false that means the directory already exists.

    此方法的返回类型为Boolean,即返回true或false,如果为true表示成功创建目录,则返回false表示目录已经存在。

Syntax:

句法:

    boolean mkdir(){
    }

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 boolean, it returns either true or false – if the directory created successfully it returns true otherwise it return false.

该方法的返回类型为boolean ,返回true或false -如果成功创建的目录返回true,否则返回false。

Java程序演示mkdir()方法的示例 (Java program to demonstrate example of mkdir() 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 CreateDirectory  {
    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 mkdir()create a new directory named 
            // C:\\Users\\Articles because directory is not 
            // exists (i.e it returns true) so this method 
            // creates empty directory.
            if (dir1.mkdir())
                System.out.println("Directory created Successfully" + " " + dir1.getName() + " ");
            else
                System.out.println("Directory already exists " + dir1.getName());

            // By using mkdir() 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.mkdir())
                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

输出量

D:\Programs>javac CreateDirectory.java

D:\Programs>java CreateDirectory
Directory created Successfully Java
Directory already exists Articles


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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值