how to convert PrintWriter to String or write to a File?

51,836

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,

Share:
51,836

Related videos on Youtube

superzoom
Author by

superzoom

Updated on July 24, 2020

Comments

  • superzoom
    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 to String?

  • superzoom
    superzoom about 12 years
    Thanks 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
    Navneeth G about 12 years
    You 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
    Navneeth G about 12 years
    When I mean write, I meant, flush the contents in the StringWriter to your response.getWriter() too.
  • Glenn Bech
    Glenn Bech over 9 years
    This is absolutely rubbish and nonsense.
  • Jubz
    Jubz over 7 years
    How is this "rubbish" or "nonsense"? I don't understand.
  • Eric Duminil
    Eric Duminil almost 7 years
    Java : never lose a good occasion to write 36 lines for (almost) nothing!
  • Yura
    Yura almost 4 years
    because it's not a subclass of PrintWriter so it doesn't interchangeable in places we type is defined as PrintWriter instead of Writer