Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Printstream to file" Watch "Printstream to file" New topic
Author

Printstream to file

Rade Koncar
Greenhorn

Joined: Sep 14, 2012
Posts: 9
Hello,

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
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

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

Joined: Sep 14, 2012
Posts: 9
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: http://aspose.com/file-tools
 
subject: Printstream to file