如何捕获 Beanshell 的输出
我一直在使用 BeanShell 来解释简单的文件,这些文件只进行一些计算,然后输出到控制台。问题是,我想获取输出。这样,从 System.out.println("test"); 我可以将 "test"
作为字符串放在其他地方。
我查看过 Interpreter.getOut(),但我还没有设法理解它的实际用途(文档没有那么有用)。我尝试使用 getOut() 抓取 PrintStream ,然后打印其内容,但它是空的。乱搞后我也尝试了以下方法:
Interpreter i = new Interpreter();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
i.setOut(ps);
i.eval("System.out.println(\"test\");");
String out = baos.toString();
但这也是空的。
I've been using BeanShell to interpret simple files that just do some calculations and then output to the console. Thing is, I want to grab the output. Such that from System.out.println("test");
I can get "test"
as a string to put somewhere else.
I've looked at Interpreter.getOut()
, but I haven't managed to understand what it's actually for (the documentation isn't that useful). I tried grabbing the PrintStream
using getOut()
and then printing its contents, but it is empty. I also tried the following after messing around:
Interpreter i = new Interpreter();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
i.setOut(ps);
i.eval("System.out.println(\"test\");");
String out = baos.toString();
But that is also empty.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)