4

I want to count how many image are in a directory. I have a copy of some code from internet which can detect the total file inside a directory.

import java.io.*;

public class CountFilesInDirectory {
      public static void main(String[] args) {
            File f = new File("/home/pc3/Documents/ffmpeg_temp/");
            int count = 0;
            for (File file : f.listFiles()) {
                    if (file.isFile()) {
                            count++;
                    }
            }
            System.out.println("Number of files: " + count);
    }
}

I wish to count a specific file type like jpg/txt. What should I do?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Eric
  • 401
  • 4
  • 13
  • 21

4 Answers4

6

Change this

if (file.isFile()) { 
  count++; 
} 

to

if (file.isFile() &&
    (file.getName().endsWith(".txt") || file.getName().endsWith(".jpg"))) { 
  count++; 
} 

What this code does is instead of just checking whether the entity denoted by file is actually a file (as opposed to a directory), you are also checking if its name ends with the particular extentions you are looking for

Note: you could use the String.toLowerCase() method to convert the name before comparison to be more accurate.

Update (based on comments): for a more OOP solution, you could implement the java.io.FilenameFilter interface and pass an instance of that to f.listFiles() -- thus enabling you to reuse that filter in another part of the program without much code repetition. If you use this approach, you need to move the endsWith logic to the FilenameFilter implementation

Attila
  • 28,265
  • 3
  • 46
  • 55
  • thanks for the help .. it works amazingly and i can continued with other parts . .thanks again for the help .. – Eric Apr 18 '12 at 01:44
  • 3
    While technically correct, I have to disagree with this solution. I would use an implementation of `java.io.FilenameFilter` and pass it into the 'File.listFiles(FilenameFilter)` method to return only those files that match a Pattern (or use `String.endsWith(String)`). This solution smells of procedural code, not OO. –  Apr 18 '12 at 02:39
  • @JohnGaughan - based on the OP's code this seemed the easiest way to modify it to get the desired result – Attila Apr 18 '12 at 02:41
  • 1
    @Attila - I agree, and your code is correct. But given the type of question, I thought it prudent to enlight the asker as well as anyone else who might not be familiar with those utility interfaces. –  Apr 18 '12 at 02:54
4

See methods like File.listFiles(FilenameFilter). Once the FilenameFilter has been created and you have the parent directory as a File, the count can be obtained using..

int filesOfInterest = parentDir.listFiles(textAndJpegFilenameFilter).length;
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

What you need is a data structure that you can use to store count of files for each extension. Map<String /*extension*/, Integer /*count*/> would be a good choice.

For each file, you will first determine its extension as everything after the last . in its name. Then check if the map has an entry for that extension. If it is there, increment it by one and if not create a new entry with count 1.

Of course, this will tell you number of files per extension, to find count of images you will need to know which extensions would have image files, which is almost impossible.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
1

The way below is how you can check the MIME type:

InputStream is = new BufferedInputStream(new FileInputStream(file));
mimeType = URLConnection.guessContentTypeFromStream(is);

Check this post for more details: Getting A File's Mime Type In Java

Even if there are not all MIME type for all kind of files, I think it'll solve your issue. This way you can ensure it's an image and keep counting.

Hope it helps! =D

Community
  • 1
  • 1
axcdnt
  • 14,004
  • 7
  • 26
  • 31