Bug 54654

Summary: Can not use same file for reading and writing a Workbook
Product: POI Reporter: Carl Volhard <CarlVolhard>
Component: POI OverallAssignee: POI Developers List <dev>
Status: CLOSED INVALID    
Severity: normal    
Priority: P2    
Version: 3.9-FINAL   
Target Milestone: ---   
Hardware: PC   
OS: Windows XP   

Description Carl Volhard 2013-03-08 11:50:56 UTC
I want to open read a workbook from a file and then safe it back to this file.

Workbook wb = WorkbookFactory.create("file.xlsx");
FileOutputStream fileOut = new FileOutputStream("file.xlsx");
wb.write(fileOut);
fileOut.close();

throws:
Exception in thread "main" org.apache.poi.POIXMLException: java.io.IOException: Can't obtain the input stream from /docProps/app.xml
	at org.apache.poi.POIXMLDocument.getProperties(POIXMLDocument.java:141)
	at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:177)
	at line wb.write(fileOut)
Caused by: java.io.IOException: Can't obtain the input stream from /docProps/app.xml
	at org.apache.poi.openxml4j.opc.PackagePart.getInputStream(PackagePart.java:500)
	at org.apache.poi.POIXMLProperties.<init>(POIXMLProperties.java:75)
	at org.apache.poi.POIXMLDocument.getProperties(POIXMLDocument.java:139)
	... 2 more
Comment 1 Nick Burch 2013-03-08 12:12:21 UTC
Windows is fussy about reading and writing to the same file, so that might be it. Does it work if you save to a different file name? Does it work if you try it on linux?
Comment 2 Carl Volhard 2013-03-12 13:14:56 UTC
Yes it works, if I take another filename. The program runs under windows, so i didn't try it on a linux system.
Is using a different file for writing the recommended way? Then it should be descriped in the docs.
Comment 3 Nick Burch 2013-03-12 17:43:48 UTC
It's a standard restriction on windows, nothing POI specific. You'll either need to use a 2nd file, or close the original one before trying to write out to it. Or switch to an OS without the restrictions!
Comment 4 Carl Volhard 2013-03-19 08:14:56 UTC
Shouldn't a Java program run on all OSs?

Can you give me some example code for opening a workbook from an existing file and writing into to it on windows?

Thanks!
Comment 5 Nick Burch 2013-03-19 11:03:40 UTC
Well written Java programs can run anywhere, but there remain a few platform specific ways you can shoot yourself in the foot...

On Windows, you either need to full close the input stream *before* trying to open it again for writing, or you need to write to another file.
Comment 6 Carl Volhard 2013-03-20 09:28:53 UTC
Thanks. Got it working.
An example for Windows in your HOWTO could be helpful.
Comment 7 Nick Burch 2013-03-20 11:17:55 UTC
Send in the text and we'll add it!