我编写了一个简单的代码来将控制台的输出打印到一个 BytreArrayOutputStream。我正在使用 JDK 1.7。但是,当我想要缓冲区为字符串时,我不能使用方法 BytreArrayOutputStream.ToString (String Charset)..
它没有这个功能。我正在使用 JDk1.7,应该支持它。我在 windows 7 中使用 Netbean。
PrintStream co1=new PrintStream(new java.io.ByteArrayOutputStream());
System.setOut(co1);
StatsUtil.submit(command);
co1.flush();
co1.close();
co1.toString();//acceptted this but it doesn't give me the stream content
String t=co1.toString("UTF-8");//the compliers give me errors the method doesn't get any string parameter
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激。
你需要调用toString()的ByteArrayOutputStream本身,而不是PrintStream它包装它。尝试使用此代码:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream co1 = new PrintStream(baos, "UTF-8");
System.setOut(co1);
StatsUtil.submit(command);
co1.flush();
co1.close();
String t = baos.toString("UTF-8");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4112 次 |
| 最近记录: |