How to list all files in a directory in java

In today’s post we are going to learn how to list all files in a directory in java.

how to list all files in a directory in java

List all files in directory

To list all files in directory we are going to use class File which has list() method which return a lazily populated Stream, the elements of which are the entries in the directory. The listing is not recursive.

Also read – linear search in java

Let’s see java program,

import java.io.*;
import java.io.File;

public class Demo
{
   public static void main(String[] args)
   {
      File fl = new File("B:/demopath");
      String strFiles[] = fl.list();
      for(String file : strFiles)
      {
         System.out.println(file);
      }
   }
}


File.listFiles() method – returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname.

import java.io.File;

public class FileDirectoryDemo 
{
   public static void main(String[] args) 
   {
      File dir = new File("B:/demopath");
      File fl[] = dir.listFiles();
      for(File file : fl)
      {
         System.out.println(file.getName());
      }
   }
}


File.listFiles(FilenameFilter filter) method – returns an array of abstract path names denoting the files and directories in the directory denoted by this abstract pathname.

import java.io.File;
import java.io.FilenameFilter;

public class FileDirectoryDemo 
{
   public static void main(String[] args) 
   {
      File directory = new File("B:/demopath");
      // retrieve only text files
      FilenameFilter txtFilter = new FilenameFilter()
      {
         @Override
         public boolean accept(File dir, String name)
         {
            if(name.endsWith(".txt"))
            {
               return true;
            }
            else
            {
               return false;
            }
         }
      };
      // listFiles() method to retrieve only text files
      File fl[] = directory.listFiles(txtFilter);
      for(File file : fl)
      {
         System.out.println(file.getName());
      }
   }
}


File.listFiles(FileFilter filter) method – returns an array of abstract pathnames denoting the files and directories in the directory denoted by this abstract pathname.

import java.io.File;
import java.io.FileFilter;

public class FileSizeDemo 
{
   public static void main(String[] args) 
   {
      File dir = new File("B:/demopath");
      // files < 10MB
      FileFilter size = new FileFilter()
      {
         @Override
         public boolean accept(File file)
         {
            if(file.length() < 10*1024*1024)
            {
               return true;
            }
            else
            {
               return false;
            }
         }
      };
      // pass listFiles() method
      File fl[] = dir.listFiles(size);
      for(File file : fl)
      {
         System.out.println(file.getName());
      }
   }
}


Java list only subdirectories from a directory

import java.io.File;

public class ListSubdirectoriesDemo 
{
   public static void main(String[] args) 
   {
      File dir = new File("B:/demopath");
      File fl[] = dir.listFiles();
      for(File file : fl)
      {
         if(file.isDirectory())
         {
            System.out.println(file.getName());
         }
      }
   }
}


List directory and its sub-directories recursively

import java.io.File;

public class ListFileRecursively 
{
   private static void listRecursively(String path)
   {
      File dir = new File(path);
      File fl[] = dir.listFiles();
      for(File file : fl)
      {
         if(file.isFile())
         {
            System.out.println(file.getName());
         }
         else if(file.isDirectory())
         {
            listRecursively(file.getAbsolutePath());
         }
      }
   }

   public static void main(String[] args) 
   {
      listRecursively("B:/demopath");
   }
}


Java 8 Files.walk() method and Files.list() method

Files.walk() method in java.nio.file.Files returns a Stream that is lazily populated with Path by walking the file tree rooted at a given starting file.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class FileNameFilter 
{
   public static void main(String[] args) throws IOException
   {
      Stream<Path> file = Files.walk(Paths.get("B:/demopath"));
      file.forEach(System.out::println);
file.close(); } }


Files.list() method in java.nio.file.Files return a lazily populated Stream, the elements of which are the entries in the directory. The listing is not recursive.

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class FilesListJava
{
public static void main(String[] args) throws IOException
{
Stream<Path> files = Files.list(Paths.get("B:/demopath"));
files.forEach(System.out::println);
files.close();
}
}