java - 尝试从 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");
}
}
解决方案
异常日志将有助于识别引发异常的行。如果没有匹配的文本,即“Summary:”,这行“paragraph.getText().split("Summary: ")[1]" 似乎会抛出提到的异常。
您可能需要打开第二个文件并验证该文本是否存在。
推荐阅读
- javascript - 解析 HTML“对象”的 CSS
- r - 具有非数值输出的热散点图
- angular - 在 Bootstrap 4.3.1 中覆盖 _variables 的正确方法是什么?
- reactjs - 使用 useRef 作为 React 钩子
- python-3.x - 1900 年之后我应该如何检查这段代码来检查闰年
- python-3.x - 匹配列并计算第三列 Np。哪里有问题
- wordpress - 如何在路由显示在 Wordpress 中之前运行函数?
- amazon-web-services - AWS Amplify 和无服务器堆栈
- java - 如何从 Java 中的 CSV 数据流中构建包含对其他对象的引用的对象
- c# - 使用实体框架向 SQL 添加具有大量列的模型的最有效方法?