Possible to share PrintWriter stream for two files?

S

Sharp

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
 
S

Sharp

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
 
J

John C. Bollinger

Sharp said:
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
(e-mail address removed)
 
S

Sharp

"John C. Bollinger"
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
 
P

Paul Lutus

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top