How do I iterate through the files in a directory in Java?

前端 未结 10 2098
星月不相逢
星月不相逢 2020-11-22 08:37

I need to get a list of all the files in a directory, including files in all the sub-directories. What is the standard way to accomplish directory iteration with Java?

10条回答
  •  礼貌的吻别
    2020-11-22 09:18

    Using org.apache.commons.io.FileUtils

    File file = new File("F:/Lines");       
    Collection files = FileUtils.listFiles(file, null, true);     
    for(File file2 : files){
        System.out.println(file2.getName());            
    } 
    

    Use false if you do not want files from sub directories.

提交回复
热议问题