Java中将标准输入和输出重定向到字节流

在Java程序中,可以通过给System.inSystem.out重新赋值来重定向标准输入和输出。

在单元测试的时候,为了对涉及到标准输入和输出的程序进行测试,常用的做法是将标准输入和输出重定向到文件,这样就可以用程序实现自动化的比对。但是这样做会有文件读取的开销,可能会降低单元测试的运行效率。

所以,更优的做法是将标准输入和输出重定向到字节流,这样就能避免文件读取的开销。

为了实现以上的效果,可以封装一个叫做getOutput的工具函数,它以字符串的形式传递标准输入内容,并以字符串的形式返回标准输出的内容。

public interface Executable {
    void execute();
}

/**
 * 重定向程序的标准输入,捕获标准输出,并以字符串的形式返回
 * @param input 标准输入
 * @param executable 待验证程序
 * @return 标准输出的内容
 */
public static String getOutput(String input, Executable executable) {
    try (
        // 字节输入流
        ByteArrayInputStream is = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8));
        // 字节输出流
        ByteArrayOutputStream os = new ByteArrayOutputStream()
    ) {
        // 保存原始的标准输入和输出
        InputStream in = System.in;
        PrintStream out = System.out;

        // 将标准输入和输出重定向到字节流
        System.setIn(is);
        System.setOut(new PrintStream(os));

        // 执行待验证的程序
        executable.execute();

        // 恢复标准输入和输出
        System.setOut(out);
        System.setIn(in);

        // 返回标准输出的内容
        return os.toString();
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
}

可以写以下的测试程序:

String output = getOutput("2 3 10 20 55 100", () -> {
    Scanner scanner = new Scanner(System.in);
    while (scanner.hasNext()) {
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        System.out.println(a + b);
    }
});
System.out.println(output);

控制台输出如下:

5
30
155
posted @ 2022-05-03 10:24  baiyuxuan  阅读(82)  评论(0编辑  收藏  举报