java mkdirs 示例_Java File mkdirs()用法及代码示例

mkdirs()方法是File类的一部分。 mkdirs()函数用于创建由抽象路径名表示的新目录以及该抽象路径名的所有不存在的父目录。如果mkdirs()函数无法创建某些目录,则它可能已经创建了其某些父目录。如果创建目录,则该函数返回true,否则返回false。

函数签名:

public boolean mkdirs()

用法:

file.mkdirs()

参数:此方法不接受任何参数。

返回值:该函数返回布尔数据类型。如果创建目录,则该函数返回true,否则返回false。

异常:如果该方法不允许创建目录,则此方法将引发SecurityException

下面的程序将说明mkdirs()函数的用法:

范例1:尝试在“f:”驱动器中创建一个名为program的新目录。

// Java program to demonstrate

// the use of File.mkdirs() method

import java.io.*;

public class GFG {

public static void main(String args[])

{

// create an abstract pathname (File object)

File f = new File("F:\\program");

// check if the directory can be created

// using the abstract path name

if (f.mkdirs()) {

// display that the directory is created

// as the function returned true

System.out.println("Directory is created");

}

else {

// display that the directory cannot be created

// as the function returned false

System.out.println("Directory cannot be created");

}

}

}

输出:

Directory is created

范例2:尝试在“f:\program”目录中创建一个名为program1的新目录,但未创建程序目录。如果目录不存在,我们将测试函数mkdirs()是否可以创建抽象路径名的父目录。

// Java program to demonstrate

// the use of File.mkdirs() method

import java.io.*;

public class GFG {

public static void main(String args[])

{

// create an abstract pathname (File object)

File f = new File("F:\\program\\program1");

// check if the directory can be created

// using the abstract path name

if (f.mkdirs()) {

// display that the directory is created

// as the function returned true

System.out.println("Directory is created");

}

else {

// display that the directory cannot be created

// as the function returned false

System.out.println("Directory cannot be created");

}

}

}

输出:

Directory is created

这些程序可能无法在在线IDE中运行。请使用离线IDE并设置文件的路径

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值