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

K

Krick

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
 
J

John C. Bollinger

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

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,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top