java input and output stream to the same file at the same time?

Discussion in 'Java' started by Krick, Aug 15, 2003.

  1. Krick

    Krick Guest

    Is it ok for me to have a FileInputStream and a FileOutputStream open
    on the same file at the same time? Should I be closing and reopening
    the file or is something happening behind the scenes that I'm not
    aware of? Here's my sample code which seems to work ok:


    String property;

    File f = new File("my.properties");
    Properties p = new Properties();

    // load the properties from the file
    try {
    p.load(new FileInputStream(f));
    }
    catch (IOException ex) {
    }

    // get the property
    property = p.getProperty("foo");

    // do stuff here, possibly modifying property

    // set the property
    p.setProperty("foo", property);

    // store the properties to the file
    try {
    p.store(new FileOutputStream(f), "MY PROPERTIES");
    }
    catch (IOException ex) {
    }



    ....
    Krick
     
    Krick, Aug 15, 2003
    #1
    1. Advertising

  2. Krick wrote:
    > Is it ok for me to have a FileInputStream and a FileOutputStream open
    > on the same file at the same time? Should I be closing and reopening
    > the file or is something happening behind the scenes that I'm not
    > aware of? Here's my sample code which seems to work ok:
    >
    >
    > String property;
    >
    > File f = new File("my.properties");
    > Properties p = new Properties();
    >
    > // load the properties from the file
    > try {
    > p.load(new FileInputStream(f));
    > }
    > catch (IOException ex) {
    > }
    >
    > // get the property
    > property = p.getProperty("foo");
    >
    > // do stuff here, possibly modifying property
    >
    > // set the property
    > p.setProperty("foo", property);
    >
    > // store the properties to the file
    > try {
    > p.store(new FileOutputStream(f), "MY PROPERTIES");
    > }
    > catch (IOException ex) {
    > }


    I think that's unwise, but I can't predict what will happen. Actually,
    I think that's unwise BECAUSE I can't predict what will happen. I in
    general think that this kind thing is unwise; the FileInputStream and
    FileOutputStream you use will be closed when they are GCd, but you can't
    predict when that will be. In the mean time they consume OS resources
    that are likely to be considerably more precious than memory, and there
    is moreover a potential for them to conflict. The minor programming
    convenience is just not worth it.


    John Bollinger
     
    John C. Bollinger, Aug 15, 2003
    #2
    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. jaivrat
    Replies:
    4
    Views:
    513
    jaivrat
    Feb 2, 2005
  2. Replies:
    2
    Views:
    457
    Larry I Smith
    May 29, 2005
  3. Replies:
    9
    Views:
    664
    Alex Buell
    Apr 27, 2006
  4. Kashif Ur Rehman
    Replies:
    2
    Views:
    888
    Tom Hawtin
    May 17, 2007
  5. scigeek
    Replies:
    7
    Views:
    380
    scigeek
    Sep 8, 2009
Loading...

Share This Page