Java创建目录的方法
在Java下如何创建目录?在 Java 中,该mkdir()函数用于创建新目录。此方法将抽象路径名作为参数,并在JavaFile类中定义。
mkdir()如果目录创建成功,则返回true ;否则,它返回false 。
句法
看看mkdir()下面的函数签名:
代码
下面的代码片段说明了该mkdir()函数创建新目录的用法:
import java.io.*;
class CreateDirectory {
public static void main(String args[])
{
// specify an abstract pathname in the File object
File f = new File("D:\\Educative");
// check if the directory can be created
// using the specified path name
if (f.mkdir() == true) {
System.out.println("Directory has been created successfully");
}
else {
System.out.println("Directory cannot be created");
}
}
}
上面的代码可能无法在在线 IDE 中运行。要编译代码,请将上述代码复制到离线 IDE 中