Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Possible to share PrintWriter stream for two files?

Reply
Thread Tools

Possible to share PrintWriter stream for two files?

 
 
Sharp
Guest
Posts: n/a
 
      10-15-2004
Hi

Consider the following:

<code>
PrintWriter out = new PrintWriter(new BufferedWriter(new
FileWriter("foo.out")));

if (//condition 1)
{
out.println("data 1"); // prints to foo.out
}
/* can I make "out" point to a new file name without closing the stream and
creating a new printwrtiter object? I doubt it's possible to share a stream
between two files.
else
{
out.println("data 2"); //prints into another file bar.out
}

Regards
Sharp





 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      10-15-2004
On Fri, 15 Oct 2004 09:46:09 GMT, Sharp wrote:

> ..I doubt it's possible to share a stream
> between two files.


What happened when you tried it?

--
Andrew Thompson
http://www.PhySci.org/codes/ Web & IT Help
http://www.PhySci.org/ Open-source software suite
http://www.1point1C.org/ Science & Technology
http://www.lensescapes.com/ Images that escape the mundane
 
Reply With Quote
 
 
 
 
Sharp
Guest
Posts: n/a
 
      10-15-2004


> On Fri, 15 Oct 2004 09:46:09 GMT, Sharp wrote:
>
> > ..I doubt it's possible to share a stream
> > between two files.

>
> What happened when you tried it?


You can't try it, so I don't know. I believe you have to close the stream
and create a new printwriter object for the file you want. If there is a way
to share the stream between two or more files without having to close the
stream then I want to know about it.

Regards
Sharp



 
Reply With Quote
 
John C. Bollinger
Guest
Posts: n/a
 
      10-15-2004
Sharp wrote:

> Hi
>
> Consider the following:
>
> <code>
> PrintWriter out = new PrintWriter(new BufferedWriter(new
> FileWriter("foo.out")));
>
> if (//condition 1)
> {
> out.println("data 1"); // prints to foo.out
> }
> /* can I make "out" point to a new file name without closing the stream and
> creating a new printwrtiter object? I doubt it's possible to share a stream
> between two files.
> else
> {
> out.println("data 2"); //prints into another file bar.out
> }


It is conceivable that you could write a custom PrintWriter that wrapped
multiple underlying streams and allowed you to switch among them. I
don't immediately see any insurmountable problems with that, but it's a
bad idea that would lead to confusing and difficult to maintain code.
There is no special advantage in logic to it, because wherever your
program must make a decision about which underlying stream the wrapper
should use, it could just as well decide instead which among the various
stream options the program should use directly.


John Bollinger
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
Sharp
Guest
Posts: n/a
 
      10-15-2004

"John C. Bollinger"
> Sharp wrote:
>
> > Hi
> >
> > Consider the following:
> >
> > <code>
> > PrintWriter out = new PrintWriter(new BufferedWriter(new
> > FileWriter("foo.out")));
> >
> > if (//condition 1)
> > {
> > out.println("data 1"); // prints to foo.out
> > }
> > /* can I make "out" point to a new file name without closing the stream

and
> > creating a new printwrtiter object? I doubt it's possible to share a

stream
> > between two files.
> > else
> > {
> > out.println("data 2"); //prints into another file bar.out
> > }

>
> It is conceivable that you could write a custom PrintWriter that wrapped
> multiple underlying streams and allowed you to switch among them. I
> don't immediately see any insurmountable problems with that, but it's a
> bad idea that would lead to confusing and difficult to maintain code.
> There is no special advantage in logic to it, because wherever your
> program must make a decision about which underlying stream the wrapper
> should use, it could just as well decide instead which among the various
> stream options the program should use directly.


OK. But imagine a long switch statement that writes data to different text
files according to different conditions. Having to create different
PrinterWriter and closing each one before creating another one does not seem
efficient. An implementation that allows you to switch to different file
names would be useful in this case.

Regards
Sharp



 
Reply With Quote
 
Paul Lutus
Guest
Posts: n/a
 
      10-15-2004
Sharp wrote:

/ ...

> OK. But imagine a long switch statement that writes data to different text
> files according to different conditions. Having to create different
> PrinterWriter and closing each one before creating another one does not
> seem efficient. An implementation that allows you to switch to different
> file names would be useful in this case.


1. Create a class that opens as many streams as required when the program
starts. IOW open all the streams at once.

2. Use a switch statement to direct the output to the desired stream.

3. At program exit, close all the streams.

This is a logical, easy-to-understand approach.

--
Paul Lutus
http://www.arachnoid.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
Confused with PrintWriter, OutputStream and OutputStreamWriter. CamT Java 1 03-16-2005 08:06 PM
What's the diff PrintWriter vs. OutputStreamWriter ? Lars Willich Java 0 01-28-2005 10:16 PM
Sending a file via PrintWriter Houman Java 2 08-18-2004 03:15 AM
Java program hangs on new PrintWriter Oz Levanon Java 1 03-01-2004 01:44 AM



Advertisments