How to list the files in current directory?

前端 未结 7 2113
傲寒
傲寒 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:49

    Had a quick snoop around for this one, but this looks like it should work. I haven't tested it yet though.

        File f = new File("."); // current directory
    
        File[] files = f.listFiles();
        for (File file : files) {
            if (file.isDirectory()) {
                System.out.print("directory:");
            } else {
                System.out.print("     file:");
            }
            System.out.println(file.getCanonicalPath());
        }
    

提交回复
热议问题