首页 > 解决方案 > 尝试从 word doc 获取特定数据时获取 ArrayIndexOutofBoundException

问题描述

我正在尝试从目录中的 word 文档列表中读取内容,并从所有文件中获取特定值,即“摘要”。下面是我的代码。当它试图获取第二个文件的值时,它会抛出Array Index out of bound 异常

有人可以帮我摆脱这个问题吗

public static void main(String[] args) throws IOException {

    File folder = new File("C:\\Kiruba\\Test Folder\\Admin\\");
    File[] listfiles = folder.listFiles();
    for(int i = 0; i < listfiles.length; i++) {
        if(listfiles[i].isFile()) {
            FileInputStream fis = new FileInputStream(listfiles[i]);
            XWPFDocument docx = new XWPFDocument(fis);
            List<XWPFParagraph> paragraphlist = docx.getParagraphs();

            for(XWPFParagraph paragraph : paragraphlist) {
                String summary = paragraph.getText().split("Summary: ")[1];
                System.out.println(summary);
            }
        }
        else
            System.out.println("There is no files in the directory");
    }    
}

标签: javaarrayslistindexoutofboundsexception

解决方案


异常日志将有助于识别引发异常的行。如果没有匹配的文本,即“Summary:”,这行“paragraph.getText().split("Summary: ")[1]" 似乎会抛出提到的异常。

您可能需要打开第二个文件并验证该文本是否存在。


推荐阅读