1

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();
        }
    }   
}
bamelam
  • 41
  • 4
  • 1
    Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – Stultuske Sep 11 '19 at 07:14
  • Possible duplicate of [ArrayIndexOutOfBoundsException bug in medata extractor lib](https://stackoverflow.com/questions/32935029/arrayindexoutofboundsexception-bug-in-medata-extractor-lib) – racraman Sep 11 '19 at 07:25
  • This post suggests your image files could be corrupted : https://stackoverflow.com/questions/32935029/arrayindexoutofboundsexception-bug-in-medata-extractor-lib – racraman Sep 11 '19 at 07:26

1 Answers1

0

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

bamelam
  • 41
  • 4