Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Blocking I/O with FileOutputStream

Reply
Thread Tools

Blocking I/O with FileOutputStream

 
 
kempshall
Guest
Posts: n/a
 
      12-07-2005
I'm working on a (trusted) applet that zips a group of files and
uploads them to a server, and also downloads the zipped file and unzips
it. As part of the unzipping process, I call a constructor to
FileOutputStream: FileOutputStream fos = new FileOutputStream( filename
);
The problem is if <filename> is open on the user's machine, this method
throws a FileNotFoundException and stops uncompressing the zip file. Is
there any way to get the FileOutputStream constructor to block until
the user closes the file?

Any help would be greatly appreciated.

--Jay

 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      12-07-2005

"kempshall" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> I'm working on a (trusted) applet that zips a group of files and
> uploads them to a server, and also downloads the zipped file and unzips
> it. As part of the unzipping process, I call a constructor to
> FileOutputStream: FileOutputStream fos = new FileOutputStream( filename
> );
> The problem is if <filename> is open on the user's machine, this method
> throws a FileNotFoundException and stops uncompressing the zip file. Is
> there any way to get the FileOutputStream constructor to block until
> the user closes the file?
>
> Any help would be greatly appreciated.


Usually people want to take problematic methods that block and make then not
block so the condition can be handled gracefully rather than the other way
around. Why not trap the exception as it is and inform the user, asking
them to close the file? Also, the exact meaning of file sharing, locking,
contention-handling, etc, is dependent on the underlying OS. You can easily
make your method block by:

FileOutputStream fos = null;
while (true) {
try {
fos = new FileOutputStream (filename);
break;
catch (FileNotFoundException ex) {
// Don't forget to handle the Interrupted Exception
Thread.sleep (2000);
}
}
// Use the file

If this is part of a larger mechanism, it may be best to pre-test the
ability to open the file before you begin.

Cheers,
Matt Humphrey http://www.velocityreviews.com/forums/(E-Mail Removed) http://www.iviz.com/


 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      12-07-2005
On 7 Dec 2005 11:01:42 -0800, "kempshall" <(E-Mail Removed)> wrote,
quoted or indirectly quoted someone who said :

>The problem is if <filename> is open on the user's machine


to a File.canWrite before you start. If you can't, do a File.exsts.
If it does tell the user to close the file in the other app or chose a
different one, then wait in a modal dialog box until the user says he
has ether fixed the problem or wants to try a different name.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
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
FileOutputStream to Different Directory bwilde@gmail.com Java 2 05-18-2006 05:22 PM
FileLock and FileInputStream/FileOutputStream bcernohous@hotmail.com Java 0 04-18-2005 06:06 PM
FileOutputStream resets the file kuhan Java 0 01-07-2004 05:10 PM
Stuck with FileOutputStream charly Java 1 12-13-2003 09:38 PM
Q FileOutputStream Jason Java 2 09-22-2003 07:16 PM



Advertisments