Untitled
unknown
plain_text
a year ago
2.2 kB
9
Indexable
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class CreateFoldersFromFile {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Nhập đường dẫn đến file txt chứa tên các thư mục
System.out.print("Nhập đường dẫn tới tệp txt (ví dụ: C:/duong/dan/toi/file.txt): ");
String filePath = scanner.nextLine();
// Đọc tên các thư mục từ file txt
List<String> folderNames = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
String line;
while ((line = br.readLine()) != null) {
folderNames.add(line.trim()); // Thêm từng dòng trong tệp vào danh sách
}
} catch (IOException e) {
System.out.println("Đã xảy ra lỗi khi đọc tệp: " + e.getMessage());
return;
}
// Kiểm tra danh sách có trống hay không
if (folderNames.isEmpty()) {
System.out.println("Tệp không chứa tên thư mục hoặc tệp rỗng.");
return;
}
// Nhập đườ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 từng dòng trong file
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