com.github.xiaoyuge5201.file.SummerNoteUtil Maven / Gradle / Ivy
package com.github.xiaoyuge5201.file;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
/**
* 使用Java自带的类从html中获取中文内容
*/
public class SummerNoteUtil extends HTMLEditorKit.ParserCallback {
StringBuffer s;
public SummerNoteUtil() {}
public void parse(Reader in) throws IOException {
s = new StringBuffer();
ParserDelegator delegator = new ParserDelegator();
// the third parameter is TRUE to ignore charset directive
delegator.parse(in, this, Boolean.TRUE);
}
public void handleText(char[] text, int pos) {
s.append(text);
}
public String getText() {
return s.toString();
}
public static void main (String[] args) {
try {
//Reader in=new StringReader("string");
FileReader in = new FileReader("java-new.html");
SummerNoteUtil parser = new SummerNoteUtil();
parser.parse(in);
in.close();
System.out.println(parser.getText());
}
catch (Exception e) {
e.printStackTrace();
}
}
}