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();
}