【问题标题】:Weird console output after temporary redirection of System.outSystem.out 临时重定向后的奇怪控制台输出
【发布时间】:2020-06-03 10:09:54
【问题描述】:

正常情况下代码如下:

String[] outputStrings =  {" Initial values: a = 1, b = 2 ", "Swapped values: a = 2, b = 1", ""};

System.out.println("array length: " + outputStrings.length);

int i = 0;
for (String line: outputStrings) {
    String str = Integer.toString(i) + " : |" + line + "| ";
    System.out.println(str);
    //System.out.flush();
    i++;
}

有以下输出:

array length: 3
0 : | Initial values: a = 1, b = 2 | 
1 : |Swapped values: a = 2, b = 1| 
2 : ||

但是,下面的代码:

CommandLineTestScaffolding scaffolding;
scaffolding = new CommandLineTestScaffolding(ExchangeCL::main);

String[] inputStrings = {"1", "2"}; // string literals with integer numbers
String[] outputStrings = scaffolding.run(inputStrings);

scaffolding = null; //no longer necessary

System.out.println("array length: " + outputStrings.length);

int i = 0;
for (String line: outputStrings) {
    String str = Integer.toString(i) + " : |" + line + "| ";
    System.out.println(str);
    //System.out.flush();
    i++;
}

有以下奇怪的输出:

array length: 1
0 : | Initial values: a = 1, b = 2 
Swapped values: a = 2, b = 1
| 

有关信息,CommandLineTestScaffolding 是临时重定向 System.out 以获取命令行输出到字符串数组的简单装置:

public class CommandLineTestScaffolding {

    private Consumer<String[]> theMethod; //Place to store main method generating text output

    public CommandLineTestScaffolding(Consumer<String[]> method) {

        theMethod = method;
    }

    public String[] run(String[] args) {

        // Create a stream to hold the output
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        PrintStream printStream = new PrintStream(outputStream);

        // A variable to temporarily hold System.out stream
        PrintStream oldOutputStream = null;

        // Critical section. System.out must be restored to its original value
        try {

            System.out.flush(); // Clear buffer;

            // IMPORTANT: Save the old System.out!
            oldOutputStream = System.out;

            // Use new wrapped byte array stream
            System.setOut(printStream);

            theMethod.accept(args); //Run the method. fill the stream

            printStream.flush(); // Clear buffer;

        }
        finally {

            // Put things back
            System.setOut(oldOutputStream);

        }

        String[] output = outputStream.toString().split("/n"); //split into lines with regex
        printStream = null;
        outputStream = null;
        return output;
    }

}

是什么导致了这种奇怪的行为?

【问题讨论】:

标签: java redirect junit console output


【解决方案1】:

outputStrings 只有一个元素,它是 "0 : | 初始值:a = 1, b = 2 交换值:a = 2,b = 1 | "

所以,这不是奇怪的行为。

您的 CommandLineTestScaffolding 无法完成您期望的工作。

但是,我不明白您需要什么?您需要与 1 完全相同的输出吗?

【讨论】:

  • 是的,我想要同样的输出。
  • outputString.split("(\\r?\\n)", -1);为我做这项工作。
  • @PavloMaistrenko 这个答案没有提到解决你的问题的解决方案(正如我从使用 split("(\\r?\\n)" 的评论中看到的那样),所以你为什么接受那个答案?你让后来的用户对不相关的答案感到困惑,你也给了免费的声誉
  • @Bashir,从技术上讲,答案并不完整,但它给了我足够的提示,足以让我自己追踪和解决问题。
  • @PavloMaistrenko 答案就是 cmets,我认为没有必要接受这个答案。正如她所说,她甚至不明白你需要什么……随便
猜你喜欢
  • 1970-01-01
  • 2011-05-28
  • 2015-06-19
  • 1970-01-01
  • 1970-01-01
  • 2013-11-14
  • 1970-01-01
  • 2020-06-03
  • 1970-01-01
相关资源
最近更新 更多