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?
The
StringReader
'stoString
method does not return theStringReader
internal buffers.You'll need to read from the
StringReader
to get this.I recommend using the overload of read which accepts a character array. Bulk reads are faster than single character reads.
ie.
You're printing out the
toString()
of the actualStringReader
object, NOT the contents of theString
that theStringReader
is reading.You need to use the
read()
and/or theread(char[] cbuf, int off, int len)
methods to read the actual chars in the String.Or using
CharStreams
from Googles Guava library:If you use the method
toString()
in aStringReader
object you will print the memory position of the object. You have yo use one of this method:read()
Reads a single character.read(char[] cbuf, int off, int len)
Reads characters into a portion of an array.Here an example:
reader.toString();
will give you the results of calling the generictoString()
method fromObject
class.You can use the
read()
method: