Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Java

Chương trình Java liệt kê tất cả file trong thư mục và thư mục con lồng nhau - Đệ quy


Để liệt kê tất cả file trong thư mục và thư mục con lồng nhau, chương trình Java như sau:

Ví dụ

import java.io.File; public class Demo{    static void print_recursively(File[] my_arr,int my_index,int sub_level){       if(my_index == my_arr.length)       return;       for (int i = 0; i < sub_level; i++)       System.out.print("\t");       if(my_arr[my_index].isFile())       System.out.println(my_arr[my_index].getName());       else if(my_arr[my_index].isDirectory()){          System.out.println("[" + my_arr[my_index].getName() + "]");          print_recursively(my_arr[my_index].listFiles(), 0, sub_level + 1);       }       print_recursively(my_arr,++my_index, sub_level);    }    public static void main(String[] args){       String path_main = "path to folder that contains files";       File main_dir = new File(path_main);       if(main_dir.exists() && main_dir.isDirectory()){          File my_arr[] = main_dir.listFiles();          System.out.println("Files listed from the main directory are : " + main_dir);          print_recursively(my_arr,0,0);       }    } }

Kết quả

Inside the directory path, all the files of all formats from the path will be listed

Lớp Demo chứa hàm print_recursively nhận mảng, giá trị index và cấp độ lồng nhau làm tham số, lặp qua các file và trả về danh sách tất cả file trong thư mục cụ thể.

Trong hàm main, đường dẫn đến thư mục chứa file được định nghĩa và đường dẫn file mới cũng được tạo. Hàm listFiles liệt kê tất cả file trong thư mục đó. Tiếp theo, hàm được gọi với các tham số này. Kết quả hiển thị trên console.