Java富文本格式去除

清空富文本内容并恢复初始格式

import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
import java.io.*;



public class RichTextHandle extends HTMLEditorKit.ParserCallback {

    private static RichTextHandle html2Text = new RichTextHandle();

    StringBuffer s;

    public RichTextHandle() {
    }


    public void parse(String str) throws IOException {
        InputStream iin = new ByteArrayInputStream(str.getBytes());
        Reader in = new InputStreamReader(iin);
        s = new StringBuffer();
        ParserDelegator delegator = new ParserDelegator();
        // the third parameter is TRUE to ignore charset directive
        delegator.parse(in, this, Boolean.TRUE);
        iin.close();
        in.close();
    }

    public void handleText(char[] text, int pos) {
        s.append(text);
    }

    public String getText() {
        return s.toString();
    }

    public static String getContent(

你可能感兴趣的:(Java,java)