title: Apache poi file getting corrupted and unable to write to existing workbook
body: <p>I am trying to write to read and write to a workbook using the following code: </p>
  
  <pre><code>public static void main(String args[]) {
      String absoluteFilePath = System.getProperty("user.dir") + File.separator + "abc.xlsx";
      System.out.println("Readin file : " + absoluteFilePath);
  
      Workbook workbook = null;
  
      try {
          workbook = WorkbookFactory.create(new File(absoluteFilePath));
  
          //reading and writing on sheets of workbook
  
  
          }
      } catch (Exception e) {
          e.printStackTrace();
      } finally {
          try {
              System.out.println("Writing to workbook and Closing the file");
              FileOutputStream fileOutputStream = new FileOutputStream(
                      new File(absoluteFilePath));
              workbook.write(fileOutputStream);
              fileOutputStream.close();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>When I run the code for the first time, I get this exception at <code>workbook.write(fileOutputStream);</code></p>
  
  <pre><code>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:148)
      at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:199)
      at NewNewDriver.main(NewNewDriver.java:129)
  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.&lt;init&gt;(POIXMLProperties.java:75)
      at org.apache.poi.POIXMLDocument.getProperties(POIXMLDocument.java:146)
      ... 2 more
  </code></pre>
  
  <p>And after this, the workbook gets corrupted and I reduced to 0kb and I get this exception on <code>WorkbookFactory.create()</code>: </p>
  
  <pre><code>org.apache.poi.poifs.filesystem.NotOLE2FileException: Invalid header signature; read 0x0000000000000000, expected 0xE11AB1A1E011CFD0 - Your file appears not to be a valid OLE2 document
      at org.apache.poi.poifs.storage.HeaderBlock.&lt;init&gt;(HeaderBlock.java:167)
      at org.apache.poi.poifs.storage.HeaderBlock.&lt;init&gt;(HeaderBlock.java:117)
      at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.&lt;init&gt;(NPOIFSFileSystem.java:225)
      at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.&lt;init&gt;(NPOIFSFileSystem.java:164)
      at org.apache.poi.poifs.filesystem.NPOIFSFileSystem.&lt;init&gt;(NPOIFSFileSystem.java:145)
      at org.apache.poi.ss.usermodel.WorkbookFactory.create(WorkbookFactory.java:105)
      at NewNewDriver.main(NewNewDriver.java:27)
  Closing the file
  Exception in thread "main" java.lang.NullPointerException
      at NewNewDriver.main(NewNewDriver.java:129)
  </code></pre>
  
  <p>Where and how should I use the FileOutputStream, workbook.write() and should I also be using FileInputStream even though I am using WorkbookFactory?</p>
  
  <p>------------EDIT----------------------I got my code working
  I used FileInputStream instead of WorkbookFactory to create the workbook and closed it after closing the FileOutputStream. That worked.</p>