Untitled
unknown
plain_text
a year ago
1.7 kB
8
Indexable
import java.io.File;
import java.util.Scanner;
public class CreateFolders {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Nhập số phần tử của mảng
System.out.print("Nhập số lượng thư mục cần tạo: ");
int numberOfFolders = scanner.nextInt();
scanner.nextLine(); // Đọc bỏ dòng mới sau khi nhập số nguyên
// Khởi tạo mảng để lưu tên thư mục
String[] folderNames = new String[numberOfFolders];
// Nhập tên cho từng thư mục
for (int i = 0; i < numberOfFolders; i++) {
System.out.print("Nhập tên cho thư mục " + (i + 1) + ": ");
folderNames[i] = scanner.nextLine();
}
// Đường dẫn thư mục gốc nơi bạn muốn tạo các thư mục con
System.out.print("Nhập đường dẫn thư mục gốc (ví dụ: C:/duong/dan/toi/folder_cha): ");
String rootPath = scanner.nextLine();
// Tạo từng thư mục với tên ứng với phần tử trong mảng
for (String folderName : folderNames) {
File folder = new File(rootPath + "/" + folderName);
if (!folder.exists()) {
if (folder.mkdir()) {
System.out.println("Tạo thư mục thành công: " + folder.getAbsolutePath());
} else {
System.out.println("Không thể tạo thư mục: " + folder.getAbsolutePath());
}
} else {
System.out.println("Thư mục đã tồn tại: " + folder.getAbsolutePath());
}
}
scanner.close();
}
}Editor is loading...
Leave a Comment