Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > java input and output stream to the same file at the same time?

Reply
Thread Tools

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

 
 
Krick
Guest
Posts: n/a
 
      08-15-2003
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
 
Reply With Quote
 
 
 
 
John C. Bollinger
Guest
Posts: n/a
 
      08-15-2003
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


 
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
confusing input and output stream behavior scigeek C++ 7 09-08-2009 07:51 PM
Conversion from Input Stream to Output Stream Kashif Ur Rehman Java 2 05-17-2007 07:50 PM
Convert DVD with subtitle stream to DivX with same subtitle stream(selectable) malise Software 2 04-17-2007 09:15 AM
How to GET multi-word input from a *file* stream as opposed to a *console* stream? sherifffruitfly@gmail.com C++ 9 04-27-2006 04:14 PM
when overloadind input/output stream operator, why return type?? jaivrat C++ 4 02-02-2005 05:18 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57