在Java中读取Word文档(.doc格式)需要借助第三方库,因为标准Java IO(java.io)本身不直接支持.doc文件的解析,本文将详细介绍如何结合Apache POI库实现.doc文件的读取,涵盖环境搭建、核心代码实现及异常处理等关键环节。

环境准备:引入Apache POI依赖
Apache POI是处理Office文档的权威开源库,支持.doc、.xls等多种格式,首先需在项目中添加POI依赖,若使用Maven,在pom.xml中配置如下:
<dependencies>
<!-- POI核心依赖 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
<!-- 处理Word文档的依赖 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>5.2.3</version>
</dependency>
</dependencies>
注意:POI 5.x版本已全面支持Java 8+,同时兼容旧版.doc格式(HWPF模块)。
基础读取流程:打开文档与获取内容
读取.doc文件的核心步骤包括:文件定位、文档对象初始化、内容提取及资源释放,以下为完整代码示例:

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class DocReader {
public static void main(String[] args) {
String filePath = "example.doc";
try (FileInputStream fis = new FileInputStream(new File(filePath));
HWPFDocument document = new HWPFDocument(fis)) {
// 获取文档文本内容
Range range = document.getRange();
String text = range.text();
System.out.println("文档内容:\n" + text);
} catch (IOException e) {
System.err.println("读取文档失败:" + e.getMessage());
}
}
}
关键点解析:
FileInputStream:用于读取文件字节流,需配合try-with-resources确保流自动关闭。HWPFDocument:POI中专门处理.doc格式的类,通过输入流初始化。Range.text():直接提取文档的全部文本内容,包含段落、表格等文本信息。
进阶操作:处理复杂文档结构
实际.doc文件可能包含表格、页眉页脚等复杂元素,需针对性处理:
读取表格内容
// 遍历文档中的表格
for (int i = 0; i < range.numTables(); i++) {
Table table = range.getTable(i);
for (int row = 0; row < table.numRows(); row++) {
for (int col = 0; col < table.getRow(row).numCells(); col++) {
System.out.print(table.getRow(row).getCell(col).text() + "\t");
}
System.out.println();
}
}
提取页眉页脚
Header header = range.getHeader();
Footer footer = range.getFooter();
System.out.println("页眉:" + header.text());
System.out.println("页脚:" + footer.text());
获取段落格式
for (int i = 0; i < range.numParagraphs(); i++) {
Paragraph paragraph = range.getParagraph(i);
System.out.println("段落:" + paragraph.text());
System.out.println("字体大小:" + paragraph.getFontName());
}
异常处理与性能优化
- 异常处理:需捕获
IOException及POIXMLException(若处理.docx文件),建议自定义异常类封装错误信息。 - 大文件处理:对于超过10MB的.doc文件,可使用
POIFSFileSystem流式读取,避免内存溢出:POIFSFileSystem fs = new POIFSFileSystem(new File("large.doc")); HWPFDocument document = new HWPFDocument(fs); - 编码问题:若文档包含非UTF-8字符,需在
FileInputStream中指定编码:new FileInputStream(new File("example.doc"), StandardCharsets.GBK);
替代方案与注意事项
- 其他库选择:若仅需提取文本,可考虑
docx4j或Apache Tika,后者支持更多格式但性能较低。 - 兼容性限制:HWPF模块对.docx支持有限,处理新版Word文档需使用
XWPFDocument(POI的.docx模块)。 - 资源释放:务必关闭所有流对象,建议使用
try-with-resources语法避免内存泄漏。
通过以上步骤,可高效实现Java中.doc文件的读取操作,实际开发中需根据文档复杂度选择合适的方法,并注意异常处理和资源管理,确保程序的稳定性和可维护性。















