File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes Printstream to file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Printstream to file" Watch "Printstream to file" New topic

Printstream to file

Rade Koncar

Joined: Sep 14, 2012
Posts: 12

I want to dynamically generate plaintext data and expose it in some generic way so it can be, for example saved to file or sent over the network.
I am not writing to a file immediately because I don't know whether my data is going to be used in file form.

Is there any easy way to convert OutputStream (PrintStream to be exact) to a plaintext file?
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Of course. Just use the PrintStream constructor which takes a File object and then the PrintStream will write to that file.
Rade Koncar

Joined: Sep 14, 2012
Posts: 12
PrintStream object is already constructed, filled with data and passed to me.

I need a way to save already constructed and filled stream object to a file. (Maybe this is not how streams work?)
I cannot write my initial stream to a file, because it wasn't decorated with file output capability, because at the time of construction
I didn't know whether it will be saved to a file.

Perhaps this cannot be done, and I shoud return my data as a POJO, and then open a stream for writing to a file?

Question is, how can I write generic OutputStream (which wasnt created with file writing capability in mind) to a file.
I agree. Here's the link:
subject: Printstream to file
It's not a secret anymore!