這幾天想用Java讀富文檔。用javax.swing.text和javax.swing.text.rtf包中的類讀RTF文檔時(shí)出現(xiàn)中文亂碼問(wèn)題(出現(xiàn)?號(hào))。
幸好找到
ANGEL SKY 的博客。用ISO8859_1編碼轉(zhuǎn)換。
代碼片斷:
String bodyText = null;
DefaultStyledDocument styledDoc = new DefaultStyledDocument(); //javax.swing.text.Document的一個(gè)實(shí)例
try {
InputStream is = new FileInputStream(new File("data/java.swing.text讀RTF文檔測(cè)試.rtf"));
new RTFEditorKit().read(is, styledDoc, 0);
bodyText = new String(styledDoc.getText(0, styledDoc.getLength()).getBytes("ISO8859_1")); //提取文本
} catch (IOException e) {
throw new DocumentHandlerException("不能從RTF中摘錄文本!", e);
} catch (BadLocationException e) {
throw new DocumentHandlerException("不能從RTF中摘錄文本!", e);
}
System.out.println(bodyText);
posted on 2008-02-01 17:05
流浪汗 閱讀(2285)
評(píng)論(0) 編輯 收藏 所屬分類:
JAVA/J2EE