此代码生成一个具有用户指定输入名称的文件夹。 创建了一个带有用户输入名称的目录,但是我希望在该目录内有子文件夹。 我已经尝试了很多次,但从未成功创建,请帮助我。
import java.io.File;
import java.util.Scanner;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Helper {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the name of the project folder : ");
String proj_name = s.nextLine();
File f = new File("c:\\"+proj_name+"\\");
if (!f.exists()) {
if (f.mkdir()) {
System.out.println("Directory is created!");
} else {
System.out.println("Failed to create directory!");
}
}
}
}
答案 0 :(得分:0)
据我对您的问题的了解,您希望一次性从用户那里获取条目并创建文件夹和子文件夹。 您可以使用file.mkdirs()一次创建所有文件夹。
public class MakingADir {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the name of the project folder : ");
String proj_name = s.nextLine();
System.out.println("Enter the sub-folder name : ");
String subFolder = s.nextLine();
String path = proj_name + File.separator + subFolder;
File f = new File("c:\\"+path+"\\");
if (!f.exists()) {
if (f.mkdirs()) {
System.out.println("Directory is created!");
} else {
System.out.println("Failed to create directory!");
}
}
}
}
希望有帮助。