我正在编写一个程序,它使用 jtidy 来清理从 URL 获取的源代码中的 html。我想在 GUI 中的 JTextArea 中显示错误和警告。我如何将警告从打印到标准输出“重新路由”到 JTextArea?我查看了 Jtidy API,没有看到任何可以满足我要求的东西。有人知道我该如何做到这一点,或者是否可能?
//测试 jtidy 选项
public void test(String U) throws MalformedURLException, IOException
{
Tidy tidy = new Tidy();
InputStream URLInputStream = new URL(U).openStream();
File file = new File("test.html");
FileOutputStream fop = new FileOutputStream(file);
tidy.setShowWarnings(true);
tidy.setShowErrors(0);
tidy.setSmartIndent(true);
tidy.setMakeClean(true);
tidy.setXHTML(true);
Document doc = tidy.parseDOM(URLInputStream, fop);
}
最佳答案
假设 JTidy 将错误和警告打印到标准输出,您可以 temporarily change where System.out
calls go :
PrintStream originalOut = System.out;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream myOutputStream = new PrintStream(baos);
System.setOut(myOutputStream);
// your JTidy code here
String capturedOutput = new String(baos.toByteArray(), StandardCharsets.UTF_8);
System.setOut(originalOut);
// Send capturedOutput to a JTextArea
myTextArea.append(capturedOutput);
有an analogous method如果您也需要对 System.err 执行此操作。
关于java - 在 GUI JTextArea 中显示 Jtidy 错误/警告消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13282367/