List the name of every file in a directory whose pathname is stored in the String variable path - Java File Path IO

Java examples for File Path IO:Directory Content

Description

List the name of every file in a directory whose pathname is stored in the String variable path

Demo Code

import java.io.File;
import java.io.IOException;

public class Main {
  public static void main(String[] arg) throws IOException {
    String path = "c:/test";
    File dir = new File(path);
    if (dir.isDirectory()) {
      File[] files = dir.listFiles();
      for (File f : files)
        System.out.println(f.getName());
    }/*from   ww w.  j a  v  a  2 s .co  m*/

  }
}

Related Tutorials