How do I convert a StringReader to a String?

Zibbobz picture Zibbobz · Jul 19, 2013 · Viewed 27.8k times · Source

I'm trying to convert my StringReader back to a regular String, as shown:

String string = reader.toString();

But when I try to read this string out, like this:

System.out.println("string: "+string);

All I get is a pointer value, like this:

java.io.StringReader@2c552c55

Am I doing something wrong in reading the string back?

Answer

ottago picture ottago · Oct 27, 2014
import org.apache.commons.io.IOUtils;

String string = IOUtils.toString(reader);