这几天想用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 
流浪汗 阅读(2293) 
评论(0)  编辑  收藏  所属分类: 
JAVA/J2EE