Counting Number of Files in a Directory in Java

Hello Friends,

Today we’ll be learning to count the number of files in a certain directory using Java.

Before beginning, we need to get familiar few methods for the same.

The Java File list() method

The list() method in Java comes under the File class in Java.

This method returns all the files in a certain directory in the form of a String array.



Syntax:

file_name.list();

Now let’s see an example of the list() method and see how it works.

In the below code we have a directory which contains some video files and respective subtitle files.

import java.io.*;
public class test  
{
  public static void main(String args[])
  {
    File file=new File("F:\\13 reasons why");//This is our directory which contains the video and subtitle files
    String[] ls=file.list(); 
    for(int i=0;i<ls.length;i++)
          {
      	System.out.println(ls[i]);
          }
  }
}

Now when the above code will be executed all the files inside the directory will be listed in the output.

13.Reasons.Why.S01E01.720p.WEBRiP.x265.ShAaNiG.srt
13.Reasons.Why.S01E02.720p.WEBRiP.x265.ShAaNiG.srt
13.Reasons.Why.S01E03.720p.WEBRiP.x265.ShAaNiG.srt
13.Reasons.Why.S01E04.720p.WEBRiP.x265.ShAaNiG.srt
13.Reasons.Why.S01E05.720p.WEBRiP.x265.ShAaNiG.srt
13.Reasons.Why.S01E06.720p.WEBRiP.x265.ShAaNiG.srt
13.Reasons.Why.S01E07.720p.WEBRiP.x265.ShAaNiG.srt
13.Reasons.Why.S01E08.720p.WEBRiP.x265.ShAaNiG.srt
13.Reasons.Why.S01E09.720p.WEBRiP.x265.ShAaNiG.srt
13.Reasons.Why.S01E10.720p.WEBRiP.x265.ShAaNiG.srt
13.Reasons.Why.S01E11.720p.WEBRiP.x265.ShAaNiG.srt
13.Reasons.Why.S01E12.720p.WEBRiP.x265.ShAaNiG.srt
13.Reasons.Why.S01E13.720p.WEBRiP.x265.ShAaNiG.srt
[fmovies.to] 13 Reasons Why - 01.mp4
[fmovies.to] 13 Reasons Why - 02.mp4
[fmovies.to] 13 Reasons Why - 03.mp4
[fmovies.to] 13 Reasons Why - 04.mp4
[fmovies.to] 13 Reasons Why - 05.mp4
[fmovies.to] 13 Reasons Why - 06.mp4
[fmovies.to] 13 Reasons Why - 07.mp4
[fmovies.to] 13 Reasons Why - 08.mp4
[fmovies.to] 13 Reasons Why - 09.mp4
[fmovies.to] 13 Reasons Why - 10.mp4
[fmovies.to] 13 Reasons Why - 11.mp4
[fmovies.to] 13 Reasons Why - 12.mp4
[fmovies.to] 13 Reasons Why - 13.mp4

So now we know how to list all the items in a certain directory, but our task is to find the number of files in the directory.

For this purpose, we will use the length property of java which is used to find the length of arrays.

Here is the complete code for our program:

import java.io.*;
public class test  
{
  public static void main(String args[])
  {
      File file=new File("F:\\13 reasons why");
      String[] ls=file.list();
      int ln=ls.length;
      System.out.println("The number of files in the directory is:"+ln);
  }
}


You can also write the above code much more efficiently in the following way performing all the important operations in a single line of code.

import java.io.*;
public class test  
{
  public static void main(String args[])
  {
      int ln=new File("F:\\13 reasons why").list().length;
      System.out.println("The number of files in the directory is:"+ln);
  }
}


 

Difference Between length and length()

Now you may have noticed that I have used the length  variable instead of the length() method in the above code, the reason is that the length variable comes under the array class, and it returns the size of the array on the other hand length() method is under the String class and it is used to find the  number of characters in the string.

But as we are using a string array in our tutorial that is why we have used the length variable and not length() method.

Hope you find the tutorial beneficial.

Have a nice day ahead.

 

Leave a Reply

Your email address will not be published. Required fields are marked *