Java - List files from a directory

By xngo on February 27, 2019

import java.io.File;
 
public class ListFilesFromDirectory
{
  public static void main(String[] args)
  {
    String sDirectoryPath = "mydirectorypath";
    File oDirectory = new File(sDirectoryPath);
    if(oDirectory.isDirectory())
    {
      File[] aFiles = oDirectory.listFiles();
      for(int i=0; i<aFiles.length; i++)
      {
        System.out.println(aFiles[i].getPath());
      }
    }
    else
    {
      System.out.println(sDirectoryPath+" is not a directory.");
    }
  }
 
}

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.