Test java programs that read from stdin and write to stdout

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.