Java File list all files and directories under a folder

Description

Java File list all files and directories under a folder

import java.io.File;

public class Main {
  public static void main(String[] args) {
    File dir = new File("C:/");
    File[] list = dir.listFiles();

    for (File f : list) {
      if (f.isFile()) {
        System.out.println(f.getPath() + " (File)");
      } else if (f.isDirectory()) {
        System.out.println(f.getPath() + " (Directory)");
      }/*from   ww  w. j  a  v a 2s . com*/
    }
  }
}



PreviousNext

Related