Java File check if a file object represents a file or a folder/directory

Description

Java File check if a file object represents a file or a folder/directory

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)");
      }//  w w  w . j a  v a2s .c om
    }
  }
}



PreviousNext

Related