开发者

Java: How can I read Reader multiple times?

If I call IOUtils.toString(reade开发者_StackOverflow社区r); it returns correct string value. Second call returns "". Reset does not supported by reader

How can I solve this situation?


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.)


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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜