aspose file tools*
The moose likes Java in General and the fly likes reading string from a Writer object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "reading string from a Writer object" Watch "reading string from a Writer object" New topic
Author

reading string from a Writer object

vijay shanker
Ranch Hand

Joined: Oct 26, 2007
Posts: 88
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

Joined: Jul 26, 2007
Posts: 9995
    
    7

Pass it as a constructor argument to the PrintWriter


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3974
    
  17

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
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 3974
    
  17

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

Joined: Oct 27, 2005
Posts: 19557
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36599
    
  16
As Bert would say . . . intermediate.

Question too difficult for beginners': moving.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reading string from a Writer object
 
Similar Threads
Hibernate One to Many Bidirectional with assigned as Primary Key
Taking the Next Step
Setting headers correctly in Javamail
Writing a FileOutputStream into a File
Trying to implement a mail queue in JavaMail using ExecutorService