How to list the files in current directory?

前端 未结 7 2117
傲寒
傲寒 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:36

    I used this answer with my local directory ( for example E:// ) it is worked fine for the first directory and for the seconde directory the output made a java null pointer exception, after searching for the reason i discover that the problem was created by the hidden directory, and this directory was created by windows to avoid this problem just use this

    public void recursiveSearch(File file ) {
     File[] filesList = file.listFiles();
        for (File f : filesList) {
            if (f.isDirectory() && !f.isHidden()) {
                System.out.println("Directoy name is  -------------->" + f.getName());
                recursiveSearch(f);
            }
            if( f.isFile() ){
                System.out.println("File name is  -------------->" + f.getName());
            }
        }
    }
    

提交回复
热议问题