• Post Reply Bookmark Topic Watch Topic
  • New Topic

FileNotFound Exception  RSS feed

 
Paras Ahuja
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I have an InputStream object reading an excel sheet and I want to write contents in the same sheet below the last row.
I use a FileOutputStream object to write into the excel sheet.
The problem is I get the following error:
java.io.FileNotFoundException: Book1.xlsx (The process cannot access the file because it is being used by another process)

I guessed it was occuring because of the InputStream object accessing the same sheet. I closed the InputStream object before writing in the sheet.
But the error still keeps bugging me. Please help...
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please PostRealCode to get better help.
 
Paras Ahuja
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try to save the output to a file different from the input one (ie. not overwrite the input file)?

I'm just guessing, but I'd say it is not OK to close the input stream you've passed to the WorkbookFactory while still using a workbook based on it.
 
Paras Ahuja
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Martin,
I actually did try to save the output to a different file (copy of my input file) and it runs perfectly without any error.
But i want to save the output in the same input file. How can i do that?
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see two possibilities:

1) Exchange the files. For example, rename the original file to something before you start processing it and delete it afterwards. A little bit safer would be to create the target file using a temporary name and then delete the original one and rename the new one to the original name.

2) Create a ByteArrayOutputStream and write the workbook into this stream. Then close input workbook and overwrite the original file with the contents of the ByteArrayOutputStream. This assumes your workbook can safely fit into your available memory.

Note: overwriting a file is always a little bit risky. If it was me, I'd prefer to have the new file saved under a new name, so that I could decide when to replace the old one with the new one. Also, what will happen if the file is processed twice by your code?
 
Paras Ahuja
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tell me what to do if i have an excel file (already filled with data) and from now on, i want to add data in the file using Apache POI. How can i do that.
Let me explain:
If i save the output in a new file, it gets saved.
But what will happen when i again read the same input file(with same records), get the last row, add data in the row below last row, and when I will try to save the data in my output file, it will overwrite the already written data in that row.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!