List all the files in a directory passed to the program as a parameter: - Java File Path IO

Java examples for File Path IO:Directory Content

Description

List all the files in a directory passed to the program as a parameter:

Demo Code

import java.io.File;

public class Main {
  public static void main(String[] args) {
    String path = args[0];// www .j  a  v  a2s  .c o  m
    File dir = new File(path);
    if (dir.isDirectory()) {
      File[] files = dir.listFiles();
      for (File f : files) {
        System.out.println(f.getName());
      }
    } else
      System.out.println("Not a directory.");
  }
}

Related Tutorials