JAVA读取RTF格式word文档

使用JAVA的RTFEditorKit类读取RTF格式Word文档时可能会遇到乱码问题,可以通过将文档内容从ISO8859-1编码转换为GBK编码来解决。具体方法是创建DefaultStyledDocument对象,读取文件内容,然后使用newString方法进行字节转换。
摘要由CSDN通过智能技术生成

JAVA利用API中自带的RTFEditorKit类对RTF格式的word文档读取过程中容易出现乱码!

可以利用new String(字符串.getBytes("ISO8859-1"),"GBK");解决文件乱码错误

import javax.swing.text.DefaultStyledDocument;
import javax.swing.text.rtf.RTFEditorKit;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.File;


String buffer = "";

//根据文件路劲创建文件,并判断文件是否存在
File file = new File(path);
if(!file.exists()){
     return buffer;
}

DefaultStyledDocument styledDoc = new DefaultStyledDocument();
// 创建文件输入流
InputStream streamReader = new FileInputStream(new File(path));
new RTFEditorKit().read(streamReader, styledDoc, 0);

//解决编码问题
buffer = new String(styledDoc.getText(0, styledDoc.getLength()).getBytes("ISO8859-1"),"GBK");
if (streamReader != null) {
     streamReader.close();
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值