• 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 异常捕获机制
    读取java当前路径的文件输出在控制台
    java 简易记事本 程序
    java 利用文件输入输出流实现文件复制
    IutputStreamWriterDemo
    OutputStreamWriterDemo
    Longest Substring Without Repeat Characters
    Add Two Numbers
    Two Sum
    使用 Canvas 绘图
  • 原文地址:https://www.cnblogs.com/baiyuxuan/p/16217784.html
Copyright © 2020-2023  润新知