glens       2004/09/21 17:42:44

  Modified:    src/examples/src/org/apache/poi/hssf/usermodel/examples
                        ReadWriteWorkbook.java
  Log:
  Even though this is just a simple example it's probably not a good idea to not close 
the streams correctly.
  
  Revision  Changes    Path
  1.6       +27 -16    
jakarta-poi/src/examples/src/org/apache/poi/hssf/usermodel/examples/ReadWriteWorkbook.java
  
  Index: ReadWriteWorkbook.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-poi/src/examples/src/org/apache/poi/hssf/usermodel/examples/ReadWriteWorkbook.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ReadWriteWorkbook.java    19 Sep 2004 02:26:30 -0000      1.5
  +++ ReadWriteWorkbook.java    22 Sep 2004 00:42:43 -0000      1.6
  @@ -39,23 +39,34 @@
       public static void main(String[] args)
           throws IOException
       {
  -        POIFSFileSystem fs      =
  -                new POIFSFileSystem(new FileInputStream("workbook.xls"));
  -        HSSFWorkbook wb = new HSSFWorkbook(fs);
  -        HSSFSheet sheet = wb.getSheetAt(0);
  -        HSSFRow row = sheet.getRow(2);
  -        if (row == null)
  -            row = sheet.createRow(2);
  -        HSSFCell cell = row.getCell((short)3);
  -        if (cell == null)
  -            cell = row.createCell((short)3);
  -        cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  -        cell.setCellValue("a test");
  +        FileInputStream fileIn = null;
  +        FileOutputStream fileOut = null;
   
  -        // Write the output to a file
  -        FileOutputStream fileOut = new FileOutputStream("workbook.xls");
  -        wb.write(fileOut);
  -        fileOut.close();
  +        try
  +        {
  +            fileIn = new FileInputStream("workbook.xls");
  +            POIFSFileSystem fs = new POIFSFileSystem(fileIn);
  +            HSSFWorkbook wb = new HSSFWorkbook(fs);
  +            HSSFSheet sheet = wb.getSheetAt(0);
  +            HSSFRow row = sheet.getRow(2);
  +            if (row == null)
  +                row = sheet.createRow(2);
  +            HSSFCell cell = row.getCell((short)3);
  +            if (cell == null)
  +                cell = row.createCell((short)3);
  +            cell.setCellType(HSSFCell.CELL_TYPE_STRING);
  +            cell.setCellValue("a test");
   
  +            // Write the output to a file
  +            fileOut = new FileOutputStream("workbook.xls");
  +            wb.write(fileOut);
  +        }
  +        finally
  +        {
  +            if (fileOut != null)
  +                fileOut.close();
  +            if (fileIn != null)
  +                fileIn.close();
  +        }
       }
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to