java 解析doc,docx,rtf格式文件代码

本文介绍如何使用Java代码解析DOC、DOCX和RTF格式的文档,涵盖使用特定库提取文本的方法,适用于需要从这些格式文件中读取内容的场景。

java 解析doc,docx,rtf格式文件代码

解析读取文件前首先将以下jar包引入程序中:
在这里插入图片描述

  1. doc 解析

String buffer="";
InputStream is = new FileInputStream(dest);
WordExtractor ex = new WordExtractor(is);
buffer = ex.getText();
System.out.println(buffer);

2.docx 解析

String buffer="";
FileInputStream fis = new FileInputStream(dest);
XWPFDocument xdoc = new XWPFDocument(fis);
XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
buffer = extractor.getText();
System.out.println(buffer);

3.rtf 解析

String result = null;
try {
DefaultStyledDocument styledDoc = new DefaultStyledDocument();
// 创建文件输入流
InputStream streamReader = new FileInputStream(dest);
new RTFEditorKit().read(streamReader, styledDoc, 0);
//以 ISO-8859-1的编码形式获取字节byte[], 并以 GBK 的编码形式生成字符串
result = new String(styledDoc.getText(0, styledDoc.getLength()).getBytes(“ISO8859-1”),“GBK”);
} catch (IOException e) {
e.printStackTrace();
} catch (BadLocationException e) {
e.printStackTrace();
}
System.out.println(result);

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值