我尝试在控制台上显示文档文件中的数据,然后收到此错误
run:
The document is really a RTF file
Exception in thread "main" java.lang.NullPointerException
at DocReader.readDocFile(DocReader.java:36)
at DocReader.main(DocReader.java:47)
Java Result: 1
BUILD SUCCESSFUL (total time: 4 seconds)
谁能解释一下我哪里出了问题
代码是 导入java.io.File; 导入java.io.FileInputStream;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.extractor.WordExtractor;
public class DocReader {
public void readDocFile() {
File docFile = null;
WordExtractor docExtractor = null ;
WordExtractor exprExtractor = null ;
try {
docFile = new File("C:\\web.doc");
FileInputStream fis=new FileInputStream(docFile.getAbsolutePath());
HWPFDocument doc=new HWPFDocument(fis);
docExtractor = new WordExtractor(doc);
}
catch(Exception exep)
{
System.out.println(exep.getMessage());
}
String [] docArray = docExtractor.getParagraphText();
for(int i=0;i<docArray.length;i++)
{
if(docArray[i] != null)
System.out.println("Line "+ i +" : " + docArray[i]);
}
}
public static void main(String[] args) {
DocReader reader = new DocReader();
reader.readDocFile();
}
}
最佳答案
The document is really a RTF file
这是来自 HWPFDocument
构造函数的 IllegalArgumentException
的典型消息。就这一点而言,这意味着提供的文件实际上是一个(写字板)RTF 文件,其.rtf
扩展名已错误地重命名为.doc
。
改为提供真实 MS Word .doc
文件并修复您的代码,以便在发生异常时不继续流程。你需要扔掉它。
关于java - 在java中显示从控制台上的文档读取文档文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4996954/