How to get contents of a folder and put into an ArrayList

后端 未结 5 1047
滥情空心
滥情空心 2020-12-14 06:10

I want to use

File f = new File(\"C:\\\\\");

to make an ArrayList with the contents of the folder.

I am not very good with buffered

5条回答
  •  情深已故
    2020-12-14 06:37

    If you want to retrieve all the files recursively, you can do something like below

    public class FileLister {
    
        static List fileList = new ArrayList();
    
        public static void main(String[] args) {
            File file = new File("C:\\tmp");
            listDirectory(file);
            System.out.println(fileList);
        }
    
        public static void listDirectory(File file) {
            if(file.isDirectory()) {
                File[] files = file.listFiles();
                for(File currFile : files) {
                    listDirectory(currFile);
                }
            }
            else {
                fileList.add(file.getPath());
            }
        }
    }
    

提交回复
热议问题