Possible to share PrintWriter stream for two files?

Discussion in 'Java' started by Sharp, Oct 15, 2004.

  1. Sharp

    Sharp Guest

    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
    Sharp, Oct 15, 2004
    #1
    1. Advertising

  2. Andrew Thompson, Oct 15, 2004
    #2
    1. Advertising

  3. Sharp

    Sharp Guest


    > 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
    Sharp, Oct 15, 2004
    #3
  4. 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
    John C. Bollinger, Oct 15, 2004
    #4
  5. Sharp

    Sharp Guest

    "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
    Sharp, Oct 15, 2004
    #5
  6. Sharp

    Paul Lutus Guest

    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
    Paul Lutus, Oct 15, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oz Levanon
    Replies:
    1
    Views:
    2,093
  2. Houman
    Replies:
    2
    Views:
    697
    Frank
    Aug 18, 2004
  3. Lars Willich
    Replies:
    0
    Views:
    585
    Lars Willich
    Jan 28, 2005
  4. CamT
    Replies:
    1
    Views:
    4,392
    Oscar kind
    Mar 16, 2005
  5. Saraswati lakki
    Replies:
    0
    Views:
    1,298
    Saraswati lakki
    Jan 6, 2012
Loading...

Share This Page