File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/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
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: 10384
    
    8

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: 4181
    
  21

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: 4181
    
  21

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: 19696
    
  20

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: 38888
    
  23
As Bert would say . . . intermediate.

Question too difficult for beginners': moving.
 
 
subject: reading string from a Writer object