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

2022/5/3 11:12:41

本文主要是介绍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


这篇关于Java中将标准输入和输出重定向到字节流的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程