• Post Reply Bookmark Topic Watch Topic
  • New Topic

How can i read the content of the Printwriter object  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
How can i read the content of the printwriter object and i want to store the content in to String. Give me Quick respnse Thank U.


TestStepResult tsestStepResult = testIterator.next();
PrintWriter writer = new PrintWriter(System.out, true);
tsestStepResult.writeTo(writer);
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
mastan vali wrote: . . . Give me Quick respnse Thank U.
. . .
We never say “quick response” in any questions. And we know how to spell you.

What do you mean by contents of a PrintWriter? Does a print writer have contents in the form of a String? It has contents in the form of its input and output connections, maybe.

If you look at the documentation (click → PrintWriter), you see it has two accessible fields, but not the String which it is supposed to write.
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will also notice the toString() method is not overridden in PrintWriter.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mastan. Welcome to the Ranch!

Short answer: you can't. You've created a PrintWriter that writes direct to System.out. It doesn't store the text you write to it, it just writes it out to System.out.

Longer answer: but if you want to create a different PrintWriter, write to that, and then get the contents as a String, you can do that. What you can do is use the StringWriter class. That's a Writer class that adds everything to a string buffer, and you can then get the contents. Once you've created that, you can wrap it in a PrintWriter.

For example:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!