I am using the following code to check if an excel file is aready open by another app:-
When the file is already open, the following error is generated which I successfully catch and handle:-
All is fine. I shut the excel spreadsheet manually then run my application again. At the point where testOut.close() is executed, above - the file content is erased completely and I am left with a 0k file!
Please would someone be able to tell me where I am going wrong?
FileOutputStream, in fact all Streams and Readers, by default open a file for writing (or overwriting, if it exists). What you want to do is open the file for appending. Have a look at the javadoc and you'll find the appropriate constructor.
I'm afraid I already tried that using the following code:
Problem is that this does not generate an error in the same way as the original code does. I want to check the file is open so I can close it. I am updating existing rows in the spreadsheet. The only thing that works is when the file is not opened by other users, and the FileOutputStream is opened using only the filename in the constructor. Appending the file (regardless of whether it is already open or closed) does not work. I'm looking for an elegant way of checking it is open before I attempt to write to it.