This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

reading string from a Writer object

 
vijay shanker
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,

I have got a Writer object


Now i need to write content at wtr to a PrintWriter.

What should i do for a optimum solution.
 
Maneesh Godbole
Saloon Keeper
Posts: 11021
12
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pass it as a constructor argument to the PrintWriter
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Writer has no means of getting at the 'contents' of the Writer so there isn't a way to get it. The Writer is usually backed by some sort of Stream, sending the data it gets to some other source, so there may actually be no 'contents' of the Writer - it may pass directly through the Writer through the backing Stream to where ever the target was meant to be.

Your only hope is to see if you can have some effect on the Writer that gets returned by the controller. If you can make the controller generate a Wrapper around the the controller's intended Writer then you have a chance.

So what environment is this controller in? Is it servlets?
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:Pass it as a constructor argument to the PrintWriter


Maybe I read the question wrong. I though he was looking at how to get what some other source was writing to the provided Writer and copy it into his PrintWriter.

Vijay, do you want to get the data FROM the Writer to copy to your PrintWriter, or do you want to make a PrintWriter that writes content to the same output as the Writer?
 
Rob Spoor
Sheriff
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:A Writer has no means of getting at the 'contents' of the Writer so there isn't a way to get it.

This is true with the exception of StringWriter. Its toString() method returns its contents.

However, it's not likely that the returned Writer is a StringWriter, and you can't turn it into one.

What you can do is create a little wrapper:

If you then use this Writer to write to instead of the original writer, you will a) write to that Writer, and b) still be able to retrieve what has been written.
 
Campbell Ritchie
Sheriff
Posts: 48635
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Bert would say . . . intermediate.

Question too difficult for beginners': moving.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic