Java List Directory

Hey guys!! Welcome to flower brackets blog. In today’s post we are going to learn java list directory.

The easiest way of doing is using the class File which offers list() method which in turn returns String-array.

Here i’m using string array files to hold all the names of contents in directory. Let’s see code implementation,

java list directory

Example: java list directory

Method 1: File.list() method – Here is the code for java list files in directory,

import java.io.*;

public class ListContentsOfDirectoryInJava
{
   public static void main(String[] args)
   {
      File fl = new File("A://Workspace");
      String files[] = fl.list();
      System.out.println("List of Contents of " + fl.getPath());
      for(int x = 0; x < files.length; x++)
      {
         System.out.println(files[x]);
      }
   }
}

Output:

List of Contents of A:\Workspace
demo.javaclass
demo.flowerbrackets

For example I’m listing all the files in the A drive using java code. Further I have created ListContentsOfDirectoryInJava class.

Also Read – Create Directory Java Program

First we have to create file object ‘fl’ to a new file and inside the braces we have to mention path.

In the next step we want to print all the files of the A drive. To do this we have a method in java called fl.list(),

String files[] = fl.list();

fl.list() will return a string array. Now we will loop through all these string array to get the names of files located in A drive.

So I have written a for loop which will print all the above files.

So this is how we get files in directory java.


Method 2 : File.listFiles() Method – This program will help you to list files in a folder or directory using file input output method.

import java.io.File;

public class ListFiles
{
   public static void main(String[] args) 
   {
      File dir = new File("A://Workspace");

      File[] fl = dir.listFiles();

      for(File file : fl) 
      {
         System.out.println(file.getName());
      }
   }
}


Method 3 : File.listFiles(FilenameFilter filter) method – Using this method we can read or filter specific files. Like .txt, .htm, .html files etc.

Here we have code,

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

public class FileNameFilter 
{
   public static void main(String[] args) 
   {
      File fl = new File("A:/Workspace");

      // here we are retrieving only html files

      FilenameFilter fnf = new FilenameFilter()
      { 
         @Override
         public boolean accept(File dir, String name)
         {
            if(name.endsWith(".htm"))
            {
               return true;
            }
            else
            {
               return false;
            }
         }
      };

      // passing "fnf" object to listFiles() method to retrieve only html files

      File[] files = fl.listFiles(fnf);

      for(File file : files) 
      {
         System.out.println(file.getName());
      }
   }
}

Output:

contact.htm
about.htm
career.htm


Method 4 : File.listFiles(FileFilter filter) method – using this method we can implement FileFilter object to retrieve files smaller than given size.

Let us see code,

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

public class FilterMethodExample 
{
   public static void main(String[] args) 
   {
      File fl = new File("A:/Workspace");

      // FileFilter class is used to retrieve the files smaller than given size

      FileFilter ff = new FileFilter() 
      { 
         @Override
         public boolean accept(File file) 
         {
            if(file.length() < 10*1024*1024)
            {
               return true;
            }
            else
            {
               return false;
            }
         }
      };

      // passing "ff" object to listFiles() method

      File[] files = fl.listFiles(ff);

      for(File file : files) 
      {
         System.out.println(file.getName());
      }
   }
}

Output:

GUI Program1
GUI Program2
GUI Program3


Method 5 : list all files in directory recursively.

Now let’s learn the best and effective way in java to get all files in directory,

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

public class RecursiveFile 
{
   void listDirectory(File dir)
   {
      File subDirectory[] = dir.listFiles(new FileFilter() 
      { 
         @Override
         public boolean accept(File pathname) 
         { 
            return pathname.isDirectory();
         }
      });

      System.out.println("\nDirectory of " + dir.getAbsolutePath());
      listFile(dir);

      for(File fl : subDirectory)
      {
         listDirectory(fl);
      }
   }

   private void listFile(File dir) 
   {
      File files[] = dir.listFiles();
      for(File file : files)
      {
         System.out.println(file.getName());
      }
   }

   public static void main(String[] args) 
   {
      new RecursiveFile().listDirectory(new File("A:/Workspace")); 
   }
}


Method 6 : list only the files in a directory.

Now let’s learn how to get all files in directory with an example,

import java.io.File;

public class FileListDemo 
{
   public static void main(String[] args) 
   {
      File dir = new File("A:/Workspace");

      File fl[] = dir.listFiles();

      for(File file : fl) 
      {
         if(file.isFile())
         {
            System.out.println(file.getName());
         }
      }
   }
}


Method 7 : list only the subdirectories in a directory.

In the below java program we are going to learn how to get only subdirectories in a directory,

import java.io.File;

public class FileDirectoryDemo 
{
   public static void main(String[] args) 
   {
      File dir = new File("A:/Workspace");

      File fl[] = dir.listFiles();

      for(File file : fl) 
      {
         if(file.isDirectory())
         {
            System.out.println(file.getName());
         }
      }
   }
}


conclusion

So in this post we learnt java get all files in directory, java get list of files in directory, java get files from directory and many more with examples.

I hope you guys have understood the concept of java list directory. You can subscribe to my blog flower brackets if you haven’t already.

Do share this post if you like.

You May Also Like