Test java programs that read from stdin and write to stdout

后端 未结 3 1612
误落风尘
误落风尘 2020-12-11 03:38

I am writing some code for a programming contest in java. The input to the program is given using stdin and output is on stdout. How are you folks testing programs that work

3条回答
  •  抹茶落季
    2020-12-11 03:58

    Try the following:

    String string = "aaa";
    InputStream stringStream = new java.io.ByteArrayInputStream(string.getBytes())
    

    stringStream is a stream that will read chars from the input string.

    OutputStream outputStream = new java.io.ByteArrayOutputStream();
    PrintStream printStream = new PrintStream(outputStream);
    // .. writes to printWriter and flush() at the end.
    String result = outputStream.toString()
    

    printStream is a PrintStream that will write to the outputStream which in turn will be able to return a string.

提交回复
热议问题