how to convert PrintWriter to String or write to a File?
Solution 1
It will depend on: how the PrintWriter is constructed and then used.
If the PrintWriter is constructed 1st and then passed to code that writes to it, you could use the Decorator pattern that allows you to create a sub-class of Writer, that takes the PrintWriter as a delegate, and forwards calls to the delegate, but also maintains a copy of the content that you can then archive.
public class DecoratedWriter extends Writer
{
private final Writer delegate;
private final StringWriter archive = new StringWriter();
//pass in the original PrintWriter here
public DecoratedWriter( Writer delegate )
{
this.delegate = delegate;
}
public String getForArchive()
{
return this.archive.toString();
}
public void write( char[] cbuf, int off, int len ) throws IOException
{
this.delegate.write( cbuf, off, len );
this.archive.write( cbuf, off, len );
}
public void flush() throws IOException
{
this.delegate.flush();
this.archive.flush();
}
public void close() throws IOException
{
this.delegate.close();
this.archive.close();
}
}
Solution 2
To get a string from the output of a PrintWriter
, you can pass a StringWriter
to a PrintWriter
via the constructor:
@Test
public void writerTest(){
StringWriter out = new StringWriter();
PrintWriter writer = new PrintWriter(out);
// use writer, e.g.:
writer.print("ABC");
writer.print("DEF");
writer.flush(); // flush is really optional here, as Writer calls the empty StringWriter.flush
String result = out.toString();
assertEquals("ABCDEF", result);
}
Solution 3
Why not use StringWriter
instead? I think this should be able to provide what you need.
So for example:
StringWriter strOut = new StringWriter();
...
String output = strOut.toString();
System.out.println(output);
Solution 4
You cannot get it with just your PrintWriter
object. It flushes the data, and does not hold any content within itself. This isn't the object you should be looking at to get the entire string,
Related videos on Youtube
superzoom
Updated on July 24, 2020Comments
-
superzoom almost 4 years
I am generating dynamic page using JSP, I want to save this dynamically generated complete page in file as archive.
In JSP, everything is written to
PrintWriter out = response.getWriter();
At the end of page, before sending response to client I want to save this page, either in file or in buffer as string for later treatment.
How can I save
Printwriter
content or convert toString
?-
Raj about 12 yearsplease check the answers for the following question. stackoverflow.com/questions/2010990/…
-
superzoom about 12 yearsThanks Raj, but this not what I am looking for, client will see normal html, but on server side, script will create local copy and write in file.
-
BalusC about 12 yearspossible duplicate of How to log response content from a java web server
-
-
superzoom about 12 yearsThanks Navneeth Gopalakrishnan, if I cant use Printwriter, ,then what option I have available ? What about response object? Because everything is going through response to client!!!
-
Navneeth G about 12 yearsYou can write things you want to write into a StringWriter and finally when everything is done, you can write it to the reponse's writer too. In that way you have the data you have written which can be used for any other purposes.
-
Navneeth G about 12 yearsWhen I mean write, I meant, flush the contents in the StringWriter to your
response.getWriter()
too. -
Glenn Bech over 9 yearsThis is absolutely rubbish and nonsense.
-
Jubz over 7 yearsHow is this "rubbish" or "nonsense"? I don't understand.
-
Eric Duminil almost 7 yearsJava : never lose a good occasion to write 36 lines for (almost) nothing!
-
Yura almost 4 yearsbecause it's not a subclass of PrintWriter so it doesn't interchangeable in places we type is defined as PrintWriter instead of Writer