简体   繁体   English

如何在读取文件时修复索引超出范围的异常(元数据提取器)

[英]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));' 'System.out.println(Arrays.toString(sourceFiles));' gives back all the 10 files in the folder. 返回文件夹中的所有10个文件。 But the 'System.out.println(x);' 但是'System.out.println(x);' gives back only 3 of the files and then gives a index 3 out bound error. 只返回3个文件,然后给出索引3出界错误。 How can I fix my code so it will read all 10 files? 如何修复我的代码,使其可以读取所有10个文件? 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). (如果我取消注释代码中的Tag部分,它将给出相同3个文件的元数据)。 Oh and I am new to java, give me as much information as possible. 哦,我是Java新手,请给我尽可能多的信息。

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();
        }
    }   
}

Never mind, I got it. 没关系,我明白了。 The error occurred because the program crashed at the 4th file, a .ini file it couldn't read. 发生错误是因为程序在第4个文件(无法读取的.ini文件)时崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM