I know how to redirect the stdout to a file, but I have no idea on how to redirect it to a string.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes – you can use a
ByteArrayOutputStream:Then you can get the string with
baos.toString().To specify encoding (and not rely on the one defined by the platform), use the
PrintStream(stream, autoFlush, encoding)constructor, andbaos.toString(encoding)If you want to revert back to the original stream, use: