這幾天想用Java讀富文檔。用javax.swing.text和javax.swing.text.rtf包中的類讀RTF文檔時出現中文亂碼問題(出現?號)。
幸好找到
ANGEL SKY 的博客。用ISO8859_1編碼轉換。
代碼片斷:
String bodyText = null;
DefaultStyledDocument styledDoc = new DefaultStyledDocument(); //javax.swing.text.Document的一個實例
try {
InputStream is = new FileInputStream(new File("data/java.swing.text讀RTF文檔測試.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
流浪汗 閱讀(2279)
評論(0) 編輯 收藏 所屬分類:
JAVA/J2EE