954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Counting number of files in a directory

Hello,
How would I count the number of files in a directory? Thanks!

christiangirl
Junior Poster
108 posts since Apr 2008
Reputation Points: 20
Solved Threads: 1
 

listFiles() and use the length of the returned array?

See the API docs for File.

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

How would I do that?

christiangirl
Junior Poster
108 posts since Apr 2008
Reputation Points: 20
Solved Threads: 1
 
masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

I did look it up, even before the first time you told me too. But I am still having trouble figuring it out. How do I used the array listFiles returns?

christiangirl
Junior Poster
108 posts since Apr 2008
Reputation Points: 20
Solved Threads: 1
 

How do you normally use an array?

String[] files = file.listFiles();
int numFiles = files.length;
masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 

oooh lol now i feel dumb. Thanks!

christiangirl
Junior Poster
108 posts since Apr 2008
Reputation Points: 20
Solved Threads: 1
 

Also note that directories will also be included and can be discerned with file.isDirectory(). If you need to include file counts from subdirectories, you'll need to do the recursion yourself.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 
Also note that directories will also be included and can be discerned with file.isDirectory(). If you need to include file counts from subdirectories, you'll need to do the recursion yourself.

I am writing a program that should return the number of files in a user specified directory. I have a sample program that returns the size of a directory. How would I go about changing it to return the number of files? (You referred to it in this post.)

/*
 * CSCI 2410
 * Meghan E. Hembree
 * Friday, September 3, 2010, 11:00:00am
 *
 * Description: (Number of files in a directory)  Write a program that
 * prompts the user to enter a directory and displays the number of the
 * files in the directory.
 *
 */
package exercise20_29;

import java.io.File;
import java.util.Scanner;

public class Exercise20_29 {

    public static void main(String[] args) {
        //Prompt the user to enter a directory or a file
        System.out.print("Enter a directory or a file: ");
        Scanner input = new Scanner(System.in);
        String file = input.nextLine();

        //Display the size
        System.out.println(getSize(new File(file)) + " files");
    }

    public static long getSize(File file){
        //Store the total size of all files
        long size = 0;

        if(file.isDirectory()){
            //All files and subdirectories
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++){
                //Recursive call
                size += getSize(files[i]);
            }
        }
        //Base case
        else{
            size += file.length();
        }

        return size;
    }

}
meglizhem
Newbie Poster
1 post since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

You can count them recursively the same way you determined the total size. I assume you don't want to include directories in that count.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You