我使用以下代码将堆栈跟踪打印到JTextArea:
try
{
throw new IOException();
}
catch(IOException e)
{
e.printStackTrace(pw);
ta1.append(sw.toString());
}
pw.flush();
sw.flush();
try
{
throw new SQLException();
}
catch(SQLException e)
{
e.printStackTrace(pw);
ta1.append(sw.toString());
}它打印出2个IOException轨迹和1个SQLExeption轨迹。为什么stringwriter没有被冲出来?
我想要一个IOException跟踪和一个SQLExeption跟踪。
请建议正确的方法来做这件事。
发布于 2015-07-07 21:48:17
StringWriter的flush方法什么也不做!flush方法就是为了与java.io.Writer兼容。
StringWriter来源:
/**
* Flush the stream.
*/
public void flush() {
}和PrintWriter调用StringWriter flush方法...
PrintWriter来源:
/**
* Flushes the stream.
* @see #checkError()
*/
public void flush() {
try {
synchronized (lock) {
ensureOpen();
out.flush();
}
}
catch (IOException x) {
trouble = true;
}
}因此,如果您想清除pw和sw,就不应该使用flush方法。您需要创建一个新的。
请参见:
https://stackoverflow.com/questions/31265615
复制相似问题