Search code examples
javaarraysindexoutofboundsexceptionmetadata-extractor

How to fix index out of bounds exception with reading files (metadata-extractor)


I made a small little program that is supposed to read metadata from files from a specific directory. The metadata is printed out in Tags. I've copied my code below. The 'System.out.println(Arrays.toString(sourceFiles));' gives back all the 10 files in the folder. But the 'System.out.println(x);' gives back only 3 of the files and then gives a index 3 out bound error. How can I fix my code so it will read all 10 files? Could someone help me with this? (if i uncomment the Tag part in the code, it will give the metadata of the same 3 files). Oh and I am new to java, give me as much information as possible.

I am using Metadata-extractor.

Thank you!

package scheme;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;

public class Test{

public static void main(String[] args) {
    // TODO Auto-generated method stub
    File image = new File("C:/Users/[username]/Pictures/");
    File[] sourceFiles = image.listFiles(); 
    System.out.println(Arrays.toString(sourceFiles));   
    Metadata metadata = null;
    try {
    for (File x: sourceFiles) {

         metadata = ImageMetadataReader.readMetadata(x);
         System.out.println(x);
        //for (Directory directory : metadata.getDirectories()) {
            //for (Tag tag : directory.getTags()) {
              // System.out.println(tag);
            //}
        //}
        }
    }
         catch (ImageProcessingException e) {
        e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        }
    }   
}

Solution

  • Never mind, I got it. The error occurred because the program crashed at the 4th file, a .ini file it couldn't read.