How to list the files in current directory?

前端 未结 7 2115
傲寒
傲寒 2021-02-05 01:05

I want to be able to list the files in the current directory. I\'ve made something that should work but doesn\'t return all the file names.

File dir = new File(\         


        
7条回答
  •  再見小時候
    2021-02-05 01:52

    Try this,to retrieve all files inside folder and sub-folder

    public static void main(String[]args)
        {
            File curDir = new File(".");
            getAllFiles(curDir);
        }
        private static void getAllFiles(File curDir) {
    
            File[] filesList = curDir.listFiles();
            for(File f : filesList){
                if(f.isDirectory())
                    getAllFiles(f);
                if(f.isFile()){
                    System.out.println(f.getName());
                }
            }
    
        }
    

    To retrieve files/folder only

    public static void main(String[]args)
        {
            File curDir = new File(".");
            getAllFiles(curDir);
        }
        private static void getAllFiles(File curDir) {
    
            File[] filesList = curDir.listFiles();
            for(File f : filesList){
                if(f.isDirectory())
                    System.out.println(f.getName());
                if(f.isFile()){
                    System.out.println(f.getName());
                }
            }
    
        }
    

提交回复
热议问题