是否可以在打印流中替换正则表达式?
我有一段代码记录了控制台窗口中显示的所有文本,但它也记录了 ANSI 转义码。
我发现这个正则表达式"s:\x1B\[[0-9;]*[mK]::g"
可以删除它们,但它只适用于字符串。有没有办法将正则表达式替换应用于恒定的字符串流并过滤掉 ANSI 转义码?
如果可能的话,尽可能地简化它,在编程方面我仍然是一个新手,我只是在一个已经存在的程序上构建。
编辑:
我有这个代码,我在堆栈溢出的其他地方找到了它,这允许我同时流式传输到日志文件和控制台。
这就是我使用的,然后我在这之后设置了发球台。
Logging tee = new Logging(file, System.out);
.
package com.md_5.mc.chat;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
public class Logging extends PrintStream
{
private final PrintStream second;
public Logging(OutputStream main, PrintStream second)
{
super(main);
this.second = second;
}
public void close()
{
super.close();
}
public void flush()
{
super.flush();
this.second.flush();
}
public void write(byte[] buf, int off, int len)
{
super.write(buf, off, len);
this.second.write(buf, off, len);
}
public void write(int b)
{
super.write(b);
this.second.write(b);
}
public void write(byte[] b) throws IOException
{
super.write(b);
this.second.write(b);
}
}