fedor.belov
fedor.belov

Reputation: 23353

Java: How can I read Reader multiple times?

If I call IOUtils.toString(reader); it returns correct string value. Second call returns "". Reset does not supported by reader

How can I solve this situation?

Upvotes: 4

Views: 2898

Answers (2)

Jeff Foster
Jeff Foster

Reputation: 44746

Use a java.io.Reader that does support reset, such as CharArrayReader (see http://download.oracle.com/javase/6/docs/api/java/io/CharArrayReader.html).

A BufferedReader also supports reset() of a limited number of characters if a mark is set.

More generally the markSupported method indicates whether the implementation of Reader you are using supports mark/reset (thanks to comment from Bala R pointing that out).

Upvotes: 3

Ernest Friedman-Hill
Ernest Friedman-Hill

Reputation: 81724

You can't make the Reader "re-readable" if it doesn't support mark() and reset(). But you could use the String returned from the call you've shown to create a StringReader any number of times, and read those as needed (or use mark() and reset() on a single instance to re-read it as needed.)

Upvotes: 4

Related Questions