javamkdirs⽰例_JavaFilemkdirs()⽤法及代码⽰例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
mkdirs方法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并设置⽂件的路径

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。